本文介绍了所有极端情况下默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的路由我想有这样的事情没有找到路由处理。

In my routing I would like to have something like not found route handler.

例如我已创建了一个映射像

For example I have created one mapping like

routes.MapRoute(
                     "default",
                     "{controller}/{action}/{id}",
                     new { controller = "Home", action = "Index", id="" }
             );

routes.MapRoute(
                     "Catchall",
                     "{*catchall}",
                     new { controller = "Home", action = "Lost" }
             );

但是,当用户插入解决类似/一/二/三/四/ BLA / BLA将与全部接收映射缓存。

but when user inserts address something like /one/two/three/four/bla/bla it will be cached with the Catchall mapping.

但是,当用户插入的东西,应与默认映射,
(如/一/二/,但这种控制器或者动作不执行)
我想这全部接收映射会接受这一要求,
因为所有其他映射失败。但不是这个,我得到一个错误。

But when user inserts something that should match with default mapping,(like /one/two/ ,but this controller or action is not implemented)I would want that Catchall mapping would accept this request,because all other mappings failed. But instead of this I get an error.

我应该重写某些映射处理程序来捕捉异常,如果控制器或者动作得到一个例外呢?

Should I override some mapping handlers to catch the exception if controller or action getting an exception?

推荐答案

这里的问题是,它不是路由的责任,以确保一/二映射到的文件的。这种责任落到视图引擎。由于一/二是一个有效的路线的,它会被选中。

The issue here is that it's not the Route's responsibility to make sure that "one/two" maps to a file. That responsibility falls to the ViewEngine. Since "one/two" is a valid route, it will be chosen.

如果你想处理一个无效的路线的错误,我会建议你做的仅仅是使用内置的ErrorHandling中页,以显示您将在全部接收所做的任何消息。

If you want to handle the error of an invalid route, what I would recommend you do is simply use the built in ErrorHandling "page" to display whatever message you would have done in the Catchall.

这篇关于所有极端情况下默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:35