IIS从我的ASP.NET MVC应用程序中抛出404错误

时间:2021-12-11 03:34:39

I've replaced an old PHP web site with a new ASP.NET MVC web site. The old page addresses no longer work. I want each to 301 (Moved Permanently) redirect to specific new addresses.

我用一个新的ASP.NET MVC网站替换了一个旧的PHP网站。旧页面地址不再有效。我希望每个301(永久移动)重定向到特定的新地址。

But, IIS 7 seems to intercept many such requests before they get to my application. I want to handle the error logging and redirections in the Application_Error() method of my Global.asax file rather than have IIS serve up generic error pages.

但是,IIS 7似乎在访问我的应用程序之前拦截了许多此类请求。我想处理我的Global.asax文件的Application_Error()方法中的错误日志记录和重定向,而不是让IIS提供通用错误页面。

How must I modify IIS to allow this?

如何修改IIS以允许此操作?

1 个解决方案

#1


1  

I think MVC does not see this as an error (i.e. raises no exception), but rather it simply does not match any of the routes, and as such lets IIS handle the 404 as it normally would. To handle this in code I would add a wildcard route at the end of your routing list.

我认为MVC并不认为这是一个错误(即没有异常),而是它根本不匹配任何路由,因此让IIS像往常一样处理404。要在代码中处理此问题,我将在路由列表的末尾添加通配符路由。

Global.asax.vb…

routes.MapRoute( _
    "FileNotFound", _
    "{*key}", _
    New With {.controller = "FileNotFound", _
              .action = "Http404"} _
)

FileNotFoundController.vb…

Function Http404(ByVal key As String) As ActionResult
    Dim RedirectId As Guid
    Select Case key
        Case "someold/path/andfile.php"
            RedirectId = New Guid("68215c26-0abe-4789-968e-0187683409b6")
        Case Else
            RedirectId = Guid.Empty
    End Select
    If Not RedirectId = Guid.Empty Then
        Response.StatusCode = Net.HttpStatusCode.MovedPermanently
        Response.RedirectLocation = Url.RouteUrl("SomeOtherRoute", New With {.id = RedirectId})
    Else
        Throw New Exception("Unable to resolve route.")
    End If
    Return Nothing
End Function

This will let you look at the intended URL and decide which target URL to redirect it to.

这将让您查看目标URL并确定将其重定向到的目标URL。

Alternatively, you could implement a custom 404 handler page and set that in IIS directly. In the code of that page/controller you could look at the intended URL and redirect as neccessary.

或者,您可以实现自定义404处理程序页面并直接在IIS中进行设置。在该页面/控制器的代码中,您可以查看预期的URL并根据需要重定向。

#1


1  

I think MVC does not see this as an error (i.e. raises no exception), but rather it simply does not match any of the routes, and as such lets IIS handle the 404 as it normally would. To handle this in code I would add a wildcard route at the end of your routing list.

我认为MVC并不认为这是一个错误(即没有异常),而是它根本不匹配任何路由,因此让IIS像往常一样处理404。要在代码中处理此问题,我将在路由列表的末尾添加通配符路由。

Global.asax.vb…

routes.MapRoute( _
    "FileNotFound", _
    "{*key}", _
    New With {.controller = "FileNotFound", _
              .action = "Http404"} _
)

FileNotFoundController.vb…

Function Http404(ByVal key As String) As ActionResult
    Dim RedirectId As Guid
    Select Case key
        Case "someold/path/andfile.php"
            RedirectId = New Guid("68215c26-0abe-4789-968e-0187683409b6")
        Case Else
            RedirectId = Guid.Empty
    End Select
    If Not RedirectId = Guid.Empty Then
        Response.StatusCode = Net.HttpStatusCode.MovedPermanently
        Response.RedirectLocation = Url.RouteUrl("SomeOtherRoute", New With {.id = RedirectId})
    Else
        Throw New Exception("Unable to resolve route.")
    End If
    Return Nothing
End Function

This will let you look at the intended URL and decide which target URL to redirect it to.

这将让您查看目标URL并确定将其重定向到的目标URL。

Alternatively, you could implement a custom 404 handler page and set that in IIS directly. In the code of that page/controller you could look at the intended URL and redirect as neccessary.

或者,您可以实现自定义404处理程序页面并直接在IIS中进行设置。在该页面/控制器的代码中,您可以查看预期的URL并根据需要重定向。