Cha*_*ell 5 reflection routing constraints asp.net-mvc-2
我有一点时间搞清楚如何正确实现我的404重定向.
如果我使用以下内容
<HandleError()> _
Public Class BaseController : Inherits System.Web.Mvc.Controller
''# do stuff
End Class
Run Code Online (Sandbox Code Playgroud)
然后页面上任何未处理的错误将加载"错误"视图,该视图效果很好. http://example.com/user/999(其中999是无效的用户ID)会在保留原始URL时抛出错误(这就是我想要的)
然而.如果有人将http://example.com/asdfjkl输入到url(其中asdfjkl是无效的控制器),则IIS将抛出通用404页面.(这不是我想要的).我需要的是上面要应用的相同内容.原始URL保留,并加载"NotFound"控制器.
我正在注册这样的路线
Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.RouteExistingFiles = False
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
routes.IgnoreRoute("Assets/{*pathInfo}")
routes.IgnoreRoute("{*robotstxt}", New With {.robotstxt = "(.*/)?robots.txt(/.*)?"})
routes.AddCombresRoute("Combres")
routes.MapRoute("Start", "", New With {.controller = "Events", .action = "Index"})
''# MapRoute allows for a dynamic UserDetails ID
routes.MapRouteLowercase("UserProfile", "Users/{id}/{slug}", _
New With {.controller = "Users", .action = "Details", .slug = UrlParameter.Optional}, _
New With {.id = "\d+"} _
)
''# Default Catch All MapRoute
routes.MapRouteLowercase("Default", "{controller}/{action}/{id}/{slug}", _
New With {.controller = "Events", .action = "Index", .id = UrlParameter.Optional, .slug = UrlParameter.Optional}, _
New With {.controller = New ControllerExistsConstraint})
''# Catch everything else cuz they're 404 errors
routes.MapRoute("CatchAll", "{*catchall}", _
New With {.Controller = "Error", .Action = "NotFound"})
End Sub
Run Code Online (Sandbox Code Playgroud)
请注意ControllerExistsConstraint?我需要做的是使用Reflection来发现控制器是否存在.
任何人都可以帮我填空吗?
Public Class ControllerExistsConstraint : Implements IRouteConstraint
Public Sub New()
End Sub
Public Function Match(ByVal httpContext As System.Web.HttpContextBase, ByVal route As System.Web.Routing.Route, ByVal parameterName As String, ByVal values As System.Web.Routing.RouteValueDictionary, ByVal routeDirection As System.Web.Routing.RouteDirection) As Boolean Implements System.Web.Routing.IRouteConstraint.Match
''# Bah, I can't figure out how to find if the controller exists
End Class
Run Code Online (Sandbox Code Playgroud)
我也想知道这个性能的影响......性能如何重要反思?如果它太多了,有没有更好的方法?
Anh*_*Ngo 10
我有一个C#解决方案,我希望它有所帮助.我抄袭了一些代码,虽然对于我的生活,我找不到我从哪里得到它.如果有人知道,请告诉我,以便我可以将其添加到我的评论中.
此解决方案不使用反射,但它查看所有应用程序错误(异常)并检查它是否是404错误.如果是,那么它只是将当前请求路由到不同的控制器.虽然我不是任何方面的专家,但我认为这个解决方案可能比反思更快.无论如何,这是解决方案,它进入你的Global.asax.cs,
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
// A good location for any error logging, otherwise, do it inside of the error controller.
Response.Clear();
HttpException httpException = exception as HttpException;
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "YourErrorController");
if (httpException != null)
{
if (httpException.GetHttpCode() == 404)
{
routeData.Values.Add("action", "YourErrorAction");
// We can pass the exception to the Action as well, something like
// routeData.Values.Add("error", exception);
// Clear the error, otherwise, we will always get the default error page.
Server.ClearError();
// Call the controller with the route
IController errorController = new ApplicationName.Controllers.YourErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以控制器会,
public class YourErrorController : Controller
{
public ActionResult YourErrorAction()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
Jus*_*tin -1
为什么不在 web.config 文件中使用自定义错误捕获它们并避免一堆反射呢?
<customErrors mode="On">
<error statusCode="404" redirect="/Error/NotFound" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)