正确的404消息缺少ASP.Net MVC控制器 [英] Correct 404 message for a missing ASP.Net MVC controller

查看:91
本文介绍了正确的404消息缺少ASP.Net MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC 2应用程序应该总是给一个好的404页。

I have an MVC 2 application that should always give a 'nice' 404 page.

不过目前我得到一个较低水平的.Net之一:在'/网站名称应用程序的服务器错误...

However currently I get a low level .Net one: "Server Error in '/sitename' Application..."

我有,有一个 NOTFOUND 的行动,将呈现漂亮的404页基本控制器。

I have a base controller that has a NotFound action that will render the nice 404 page.

缺少动作的处理方式:

protected override void HandleUnknownAction(string actionName)
{
    this.NotFound(actionName).ExecuteResult(this.ControllerContext);
}

所以要 {}网站/ ValidController / NotAnAction 被正确地路由,访问

不过一到访问{}网站/ NotAController 没有。​​

我已经设置了一个包罗万象的路线:

I have routes set up with a catch all:

routes.MapRoute(
    "MVC routes",
    "{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional });

routes.MapRoute(
    "Catch All", 
    "{*url}",
    new { controller = "System", action = "NotFound" });

美中不足的所有正确捕获不匹配的路由。

The catch all correctly catches routes that don't match.

所以 {}网站/无效/动作/ ID /额外通过捕捉正确地路由所有。

So {site}/Invalid/Action/id/extra is correctly routed via the catch all.

{}网站/无效被通过MVC路线的路线和ASP.Net拿起去寻找 InvalidController ,并抛出一个异常哑当它没有找到它。

However {site}/Invalid gets picked up via the "MVC routes" route and ASP.Net goes looking for InvalidController, and throws a dumb exception when it doesn't find it.

我知道我可以在的web.config 级别覆盖这一点,但只是重定向到该页面。我想知道,当路由模式相匹配,但该控制器是不是一个有效的控制器名称。

I know that I can override this at the web.config level, but that just redirects to the page. I'd like to know when the route pattern matches but the controller is not a valid controller name.

在哪里可以赶上并改变这种行为?

Where can I catch and change this behaviour?

推荐答案

我终于找到了答案,但它仍然不理想。

I finally found the answer to this, though it's still not ideal.

您可以限制被允许符合使用正则表达式的路由控制器的名称,因此,如果我们假设控制器工厂的默认实现,我们可以找出所有受支持的可能的类名称:

You can restrict the controller names that are allowed to match a route using a regex, so if we assume the default implementation of the controller factory we can figure out all the possible class names that are supported:

// build up a list of known controllers, so that we don't let users hit ones that don't exist
var allMvcControllers = 
    from t in typeof(Global).Assembly.GetTypes()
    where t != null &&
        t.IsPublic &&
        !t.IsAbstract &&
        t.Name.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) &&
        typeof(IController).IsAssignableFrom(t)
    select t.Name.Substring(0, t.Name.Length - 10);

// create a route constraint that requires the controller to be one of the reflected class names
var controllerConstraint = new
{
    controller = "(" + string.Join("|", allMvcControllers.ToArray()) + ")"
};

// default MVC route
routes.MapRoute(
    "MVC",
    "{controller}/{action}/{id}",
    new { action = "Index", id = UrlParameter.Optional },
    controllerConstraint);

// fall back route for unmatched patterns or invalid controller names
routes.MapRoute(
    "Catch All", 
    "{*url}",
    new { controller = "System", action = "NotFound" });

这是不理想的,它增加了对应用程序启动一个打击,仍觉得过于复杂,但它确实有预期的效果。

This isn't ideal, it adds a hit on the application start and still feels far too complicated, but it does have the desired effect.

这篇关于正确的404消息缺少ASP.Net MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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