Old Classic ASP pages getting caught in MVC Route

2019-05-29 10:58发布

问题:

I am converting an existing classic ASP website to (VB) MVC and I don't want any of the existing URLs to break. I have read many posts (like this one: Routing Classic ASP Requests To .NET - SEO Redirects) about how to do the proper 301 redirect. With the latest MVC release, I've gathered that

Response.RedirectPermanent(objRedirect.new_url, True)

is all that is needed.

I have entered all of my old URLs in a database table with a corresponding column of the new URL. I have added code in my custom 404 page to get the original URL:

Dim strURL As String = Request.RawUrl.Substring(Request.RawUrl.IndexOf("aspxerrorpath=") + 15).ToLower()

so I can look it up in the database. (Interesting sidenote, MSDN's documentation here - Redirect Mode - seems to say that if I set RedirectMode=ResponseRewrite in the CustomErrors section of my web.config, I won't have to worry about doing the above, but when I've tried that, I get IIS errors saying it won't serve an ASP page?!?!?)

The problem I am encountering is that any of my old, Classic ASP URLs that have the same directory as a new MVC route are somehow being partially routed. For example, "/test/default.asp" shows up as "/test/test" in the above strURL variable of my error page.

I do have a route setup for "test":

routes.MapRoute("Test", _
                "test/{action}", _
                New With {.controller = "test", .action = "index"})

in Global.aspx.vb, but I also have tried every conceivable way to ignore all ASP pages in routes (seemingly to no avail). Here are the attempts I've made (I did see one old - 2008 - post by Phil Haack that said I could only have one of these "catch all" ignore routes, but don't know if that's still valid or not?):

routes.IgnoreRoute("{file}.asp")
routes.Ignore("{resource}.asp/{*pathInfo}")
routes.IgnoreRoute("{resource}.asp/{*pathInfo}")
routes.Add(New Route("{resource}.asp/{*pathInfo}", New StopRoutingHandler()))

and none of them seemed to make any difference (I tried them all one at a time obviously).

This isn't isolated to just one route either - it occurs for any directories that existed on the old site that match a named route on the new site.

Thanks in advance for any and all suggestions you have!

UPDATE: Here are 2 more "issues" I've discovered:

  1. I am loosing the original querystring. So, if product.asp?id=1 is requested, all I have in the error page is product.asp (any idea how to get at/save the original querystring?)
  2. Another one of my routes looks like this:

    routes.MapRoute("IndependentSales", _ "independentsales/{action}", _ New With {.controller = "independentsales", .action = "index"})

and when I request "/resale/default.asp", it goes to "resale/independentsales". WHAT is up with that???

回答1:

Have you tried to use web.config?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Remove index.php" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The example above is used to rewrite a CodeIgniter app to omit the index.php file from the URL. You can do many things with it, considering the conditions part. In this case, I only do the rewriting if it's not a file and not a directory.



回答2:

OK, after hours and hours of reading/researching/debugging, I came to the conclusion that I wasn't doing anything wrong and that there was something "buggy" in Visual Studio, .NET, MVC ... something! And I was right! I created a new project from scratch, copied all my code over there and everything worked as it should!

(Well, I'm not sure "as it should" is correct, but I did get my code to function the way I wanted. Requests for ASP pages still hit the MvcApplication_BeginRequest - which I don't think they should with my IgnoreRoute entries???? - but at least the extension was still there when it got there, so I could look-up the ASP page in my database and do the redirect!)

So, this may have been unique to me, but maybe I can save others hours of frustration ... if you think you have everything correct, maybe you do! Create a new project and try that!

... It sucks to be a programmer! ;-D

(Musings ... The 3 things that I didn't add back into my new project were:

  1. the logging tool ELMAH
  2. the RouteMagic DLL
  3. and the RouteDebugger.dll.

I may try subtracting them from my original project and see how that goes. Given the popularity of ELMAH and who wrote the route ones, I can't imagine that there would be a problem with them, but who knows ...?)