如何捕获所有路径来处理“404页面未找到”的ASP查询。净MVC吗?

时间:2022-05-24 03:29:18

Is it possible to create a final route that catches all .. and bounces the user to a 404 view in ASP.NET MVC?

是否有可能创建一条最终的路线来捕获所有的鱼。并将用户跳转到ASP中的404视图。净MVC吗?

NOTE: I don't want to set this up in my IIS settings.

注意:我不想在IIS设置中设置这个。

8 个解决方案

#1


68  

Found the answer myself.

自己找到了答案。

Richard Dingwall has an excellent post going through various strategies. I particularly like the FilterAttribute solution. I'm not a fan of throwing exceptions around willy nilly, so i'll see if i can improve on that :)

Richard Dingwall有一篇出色的文章介绍了各种策略。我特别喜欢FilterAttribute解决方案。我不喜欢把事情弄得一团糟,所以我想看看我能不能在这一点上有所改进:)

For the global.asax, just add this code as your last route to register:

对全球。asax,将此代码作为您注册的最后路径:

routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "StaticContent", action = "PageNotFound" }
    );

#2


18  

This question came first, but the easier answer came in a later question:

这个问题先出现,但更容易的答案是后面的问题:

Routing for custom ASP.NET MVC 404 Error page

定制ASP的路由。NET MVC 404错误页面

I got my error handling to work by creating an ErrorController that returns the views in this article. I also had to add the "Catch All" to the route in global.asax.

通过创建一个返回本文中的视图的ErrorController,我实现了错误处理。我还必须在global.asax航线上添加“捕捉一切”。

I cannot see how it will get to any of these error pages if it is not in the Web.config..? My Web.config had to specify:

如果不是在Web.config.. .中,我看不出它将如何到达这些错误页面。我的网络。配置指定:

customErrors mode="On" defaultRedirect="~/Error/Unknown"

and then I also added:

然后我补充说

error statusCode="404" redirect="~/Error/NotFound"

Hope this helps.

希望这个有帮助。

I love this way now because it is so simple:

我喜欢这种方式,因为它很简单:

 <customErrors mode="On" defaultRedirect="~/Error/" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="~/Error/PageNotFound/" />
 </customErrors>

#3


3  

This might be a problem when you use

当您使用时,这可能是一个问题

throw new HttpException(404);

When you want to catch that, I don't know any other way then editing your web config.

当您想要捕捉它时,我不知道有什么其他方法可以编辑您的web配置。

#4


3  

Add this lines under your project root web.config File.

在项目根web下添加这一行。配置文件。

 <system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" responseMode="ExecuteURL" path="/Test/PageNotFound" />
  <remove statusCode="500" />
  <error statusCode="500" responseMode="ExecuteURL" path="/Test/PageNotFound" />
</httpErrors>
<modules>
  <remove name="FormsAuthentication" />
</modules>

#5


3  

Also you can handle NOT FOUND error in Global.asax.cs as below

也可以在Global.asax中处理未发现的错误。cs如下

protected void Application_Error(object sender, EventArgs e)
{
    Exception lastErrorInfo = Server.GetLastError();
    Exception errorInfo = null;

    bool isNotFound = false;
    if (lastErrorInfo != null)
    {
        errorInfo = lastErrorInfo.GetBaseException();
        var error = errorInfo as HttpException;
        if (error != null)
            isNotFound = error.GetHttpCode() == (int)HttpStatusCode.NotFound;
    }
    if (isNotFound)
    {
        Server.ClearError();
        Response.Redirect("~/Error/NotFound");// Do what you need to render in view
    }
}

#6


1  

An alternative to creating a catch-all route is to add an Application_EndRequest method to your MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer.

创建“一网打尽”路由的另一种选择是,根据Marco优于独角兽的MVC 404答案,向MvcApplication添加Application_EndRequest方法。

#7


0  

If the route cannot be resolved, then MVC framework will through 404 error.. Best approach is to use Exception Filters ... Create a custom exceptionfilter and make like this..

如果路径不能被解析,那么MVC框架将会通过404错误。最好的方法是使用异常过滤器……创建一个自定义的exceptionfilter并创建如下所示。

public class RouteNotFoundAttribute : FilterAttribute, IExceptionFilter {
    public void OnException(ExceptionContext filterContext) {
        filterContext.Result  = new RedirectResult("~/Content/RouteNotFound.html");
   }
}

#8


0  

Inside RouterConfig.cs add the follwing piece of code:

RouterConfig内部。cs添加了以下代码:

  routes.MapRoute(
           name: "Error",
           url: "{id}",
           defaults: new
           {
               controller = "Error",
               action = "PageNotFound"

           });

#1


68  

Found the answer myself.

自己找到了答案。

Richard Dingwall has an excellent post going through various strategies. I particularly like the FilterAttribute solution. I'm not a fan of throwing exceptions around willy nilly, so i'll see if i can improve on that :)

Richard Dingwall有一篇出色的文章介绍了各种策略。我特别喜欢FilterAttribute解决方案。我不喜欢把事情弄得一团糟,所以我想看看我能不能在这一点上有所改进:)

For the global.asax, just add this code as your last route to register:

对全球。asax,将此代码作为您注册的最后路径:

routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "StaticContent", action = "PageNotFound" }
    );

#2


18  

This question came first, but the easier answer came in a later question:

这个问题先出现,但更容易的答案是后面的问题:

Routing for custom ASP.NET MVC 404 Error page

定制ASP的路由。NET MVC 404错误页面

I got my error handling to work by creating an ErrorController that returns the views in this article. I also had to add the "Catch All" to the route in global.asax.

通过创建一个返回本文中的视图的ErrorController,我实现了错误处理。我还必须在global.asax航线上添加“捕捉一切”。

I cannot see how it will get to any of these error pages if it is not in the Web.config..? My Web.config had to specify:

如果不是在Web.config.. .中,我看不出它将如何到达这些错误页面。我的网络。配置指定:

customErrors mode="On" defaultRedirect="~/Error/Unknown"

and then I also added:

然后我补充说

error statusCode="404" redirect="~/Error/NotFound"

Hope this helps.

希望这个有帮助。

I love this way now because it is so simple:

我喜欢这种方式,因为它很简单:

 <customErrors mode="On" defaultRedirect="~/Error/" redirectMode="ResponseRedirect">
    <error statusCode="404" redirect="~/Error/PageNotFound/" />
 </customErrors>

#3


3  

This might be a problem when you use

当您使用时,这可能是一个问题

throw new HttpException(404);

When you want to catch that, I don't know any other way then editing your web config.

当您想要捕捉它时,我不知道有什么其他方法可以编辑您的web配置。

#4


3  

Add this lines under your project root web.config File.

在项目根web下添加这一行。配置文件。

 <system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" responseMode="ExecuteURL" path="/Test/PageNotFound" />
  <remove statusCode="500" />
  <error statusCode="500" responseMode="ExecuteURL" path="/Test/PageNotFound" />
</httpErrors>
<modules>
  <remove name="FormsAuthentication" />
</modules>

#5


3  

Also you can handle NOT FOUND error in Global.asax.cs as below

也可以在Global.asax中处理未发现的错误。cs如下

protected void Application_Error(object sender, EventArgs e)
{
    Exception lastErrorInfo = Server.GetLastError();
    Exception errorInfo = null;

    bool isNotFound = false;
    if (lastErrorInfo != null)
    {
        errorInfo = lastErrorInfo.GetBaseException();
        var error = errorInfo as HttpException;
        if (error != null)
            isNotFound = error.GetHttpCode() == (int)HttpStatusCode.NotFound;
    }
    if (isNotFound)
    {
        Server.ClearError();
        Response.Redirect("~/Error/NotFound");// Do what you need to render in view
    }
}

#6


1  

An alternative to creating a catch-all route is to add an Application_EndRequest method to your MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer.

创建“一网打尽”路由的另一种选择是,根据Marco优于独角兽的MVC 404答案,向MvcApplication添加Application_EndRequest方法。

#7


0  

If the route cannot be resolved, then MVC framework will through 404 error.. Best approach is to use Exception Filters ... Create a custom exceptionfilter and make like this..

如果路径不能被解析,那么MVC框架将会通过404错误。最好的方法是使用异常过滤器……创建一个自定义的exceptionfilter并创建如下所示。

public class RouteNotFoundAttribute : FilterAttribute, IExceptionFilter {
    public void OnException(ExceptionContext filterContext) {
        filterContext.Result  = new RedirectResult("~/Content/RouteNotFound.html");
   }
}

#8


0  

Inside RouterConfig.cs add the follwing piece of code:

RouterConfig内部。cs添加了以下代码:

  routes.MapRoute(
           name: "Error",
           url: "{id}",
           defaults: new
           {
               controller = "Error",
               action = "PageNotFound"

           });