Stop exception from being thrown for non-existent controller

拈花ヽ惹草 提交于 2019-12-09 09:32:17

问题


Ok, so I am stumped on this issue. I have seen a lot of things that are supposed to resolve this issue, but I am not getting a resolution that can fulfill my requirements.

I am using ELMAH to log exceptions and am getting this exception when either a URL with invalid controller or proper controller and invalid action.

System.Web.HttpException: The controller for path '/BadController' was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I want to stop this exception from being thrown. I am using custom errors and they work fine, as the application will send to my custom 404 error page. However, the exception still gets logged.

I have the [HandleError] attribute decorating all controllers. Is this possible? And if so I would appreciate any help.

I have tried, setting a final route that redirects, but it is matching a route previous to that for whatever reason. I have attempted to use a httpModule, and clearing in the Application_Error event of the Global.asax file. Nothing is stopping that exceptions.

Thanks again for any help!


回答1:


Create a RouteConstraint, that checks if the controller exists (in fact, scans all possible controllers in beginning and caches it, you know what i mean) and add it to your standard route.

so,when you then have a request to a bad controller, it is not accepted by the normal Route. but then accepted by the catchall -> 404 route...




回答2:


Wire up a custom controller factory that can execute your desired features when it fails to locate the requested controller. It's just a few lines of code, nothing too painful.

You can roll your own controller factory, or use one of the many libraries as a starting point. You'll have a class that implements IControllerFactory and inside the CreateController() function will be your custom logic.




回答3:


Was probably not relaying my issue properly, after tons of digging around this exception is supposed to be thrown. The answer is to exclude those exceptions from ELMAH, according to this article.

By default, ELMAH logs the details of every unhandled exception, including 404 and other HTTP errors. You can instruct ELMAH to ignore these or other types of errors using error filtering.




回答4:


I use DSO's ITCloud Contrib. None of that {controller}/{action} generic parameters. Explicit UrlRoute attributes for each action method. I like seeing the parameter constraints directly above the method.



来源:https://stackoverflow.com/questions/3122021/stop-exception-from-being-thrown-for-non-existent-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!