gpt4 book ai didi

asp.net-mvc - 更正缺少 ASP.Net MVC Controller 的 404 消息

转载 作者:行者123 更新时间:2023-12-01 11:57:20 25 4
gpt4 key购买 nike

我有一个 MVC 2 应用程序,它应该总是提供一个“不错”的 404 页面。

但是目前我得到一个低级别的 .Net:“'/sitename' 应用程序中的服务器错误...”

我有一个基本 Controller ,它有一个 NotFound Action ,可以呈现漂亮的 404 页面。

处理缺失的 Action :

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

因此对 {site}/ValidController/NotAnAction 的访问被正确路由。

但是访问 {site}/NotAController 不会。

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

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

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

catch all 正确捕获不匹配的路由。

因此 {site}/Invalid/Action/id/extra 通过 catch all 正确路由。

但是 {site}/Invalid 通过“MVC routes”路由被拾取并且 ASP.Net 去寻找 InvalidController,并且当它没有时抛出一个愚蠢的异常找到它。

我知道我可以在 web.config 级别覆盖它,但这只会重定向到页面。我想知道路由模式何时匹配但 Controller 不是有效的 Controller 名称。

我在哪里可以捕捉并改变这种行为?

最佳答案

我终于找到了这个问题的答案,虽然它仍然不理想。

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

// 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" });

这并不理想,它在应用程序启动时增加了一个命中并且仍然感觉太复杂了,但它确实达到了预期的效果。

关于asp.net-mvc - 更正缺少 ASP.Net MVC Controller 的 404 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5701922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com