抛出404错误,当路由不满意 [英] throw 404 error when route is not satisfied

查看:248
本文介绍了抛出404错误,当路由不满意的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用默认路由

  routes.MapRoute(
      "Admin",                                              // Route name
      "Admin/{action}",                           // URL with parameters
      new { controller = "Admin", action = "Index" }  // Parameter defaults
  );

很多路线(约90%)为我们的应用程序都工作,所以这很好。

lots of routes (approx. 90%) for our application are working, so that's fine.

现在,当用户输入 /登录/ strangeroute ,MVC offcourse抛出一个错误,因为我没有在 strangeroute 在我的行动 logoncontroller

Now when the user enters /logon/strangeroute, MVC offcourse throws an error because i don't have the strangeroute action in my logoncontroller.

使用捕获所有异常处理程序的global.asax.cs ,我找不到让路由错误的区别(我称之为路由错误)和其他错误(在code其余抛出)。

Using the catch all exception handler in the global.asax.cs, i can't find to get the difference between routing errors (i call this a routing error) and another error (thrown in the rest of the code).

我想betweek的错误类型来区分,因为当路线给出了一个错误它的大部分时间是404错误,我希望将用户重定向到404页面,而不是正常的,通用的,错误页。

I would like to discriminate betweek the types of errors because when the route gives an error it most of the time is a 404 error, and i want to redirect the user to the 404 page and not to the normal, generic, error page.

一soultion是创建的所有路由所有页面和罢免现有默认路由和实施一个包罗万象的路线,但我想preFER不创建每个URL一个单独的路径。

One soultion would be to create all the routes for all the pages and dismiss the existing default route and implement a catch-all route but i'd prefer not to create a separate route for every url.

是否有这样做的另一种方式?

Is there another way of doing this?

推荐答案

测试此的异常你抓住是 HttpException ,如果是使用 GetHttp它code 的方法,以确定它是404

Test if the Exception you caught is an HttpException and if it is use the GetHttpCode method on it to see if it is 404.

这篇关于抛出404错误,当路由不满意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆