ASP.NET MVC – 使用Reflection查找控制器是否存在
我有一点时间搞清楚如何正确实现我的404重定向.
如果我使用以下内容 <HandleError()> _ Public Class BaseController : Inherits System.Web.Mvc.Controller ''# do stuff End Class 然后页面上任何未处理的错误将加载“错误”视图,该视图效果很好. 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,_ 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 注意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 我也想知道这个性能的影响……性能如何重要反思?如果它太多了,有没有更好的方法? 解决方法
我有一个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,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)); } } } 所以控制器会, public class YourErrorController : Controller { public ActionResult YourErrorAction() { return View(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET – Ajax Calendar Extender CSS继承自表
- asp.net – 当你不能使用ViewState时你会怎么做?
- 如何在kendo UI MVC菜单中添加字体真棒图标?
- ASP.NET MVC集成EntLib实现“自动化”异常处理[实现篇]
- ASP.NET vNext Core CLR缺少type.IsPrimitive
- asp.net-mvc – 在使用Asp.Net MVC ActionLink,RedirectToA
- asp.net-identity-2 – GenerateEmailConfirmationToken()究
- asp.net-mvc-3 – 以下部分已定义,但尚未为布局页面呈现
- asp.net版本的timthumb php类
- LoginView中的ASP.NET LoginStatus不会触发LoggingOut事件