.NET MVC or MVCContrib throws Exceptions for non-existent routes?
I'm wondering if this is normal behavior. I recently added logging inside the Application_Error method and I'm seeing exceptions being logged whenever a non-existent route is hit. This is obviously not exceptional, and certainly not good for performance and other reasons.
I read this post but I am definitely compiled in Release so that doesn't appear to be my problem.
Here is a copy of the s开发者_StackOverflowtack trace.
[2011-08-23 09:50:56,156] (7) |ERROR| - An unexpected exception occurred during an MVC application request
REQUEST: GET http://XX.XX.XX.XX/pma/scripts/setup.php
--==EXCEPTION (1)==--
TYPE: HttpException
MESSAGE:Could not find a type for the controller name 'pma'
STACKTRACE
at MvcContrib.ControllerFactories.IoCControllerFactory.CreateController(RequestContext context, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass6.<BeginProcessRequest>b__2()
at System.Web.Mvc.SecurityUtil.<>c__DisplayClassb`1.<ProcessInApplicationTrust>b__a()
at System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust[TResult](Func`1 func)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Basically this is a botnet or something, but the problem is that it is trying to look up a controller for the route and throwing an exception. Is this normal for either MVC or MVCContrib. Is there a way to prevent it?
If the request comes from a bot, this has nothing to do with MVCContrib. It's just that you are using a custom controller factory and so all requests (even the spam ones) go through that factory. There is not much you could do from within your application code about it. In order to block those requests you might need to setup firewall rules which block the spam domains those requests are coming from. Or block them based on some other rules before they hit the web server. It is off-topic for this site.
Looking at the MvcContrib IoCControllerFactory code more closely I can see that when a controllerType isn't found it does:
if (controllerType == null)
throw new HttpException(404, string.Format("Could not find a type for the controller name '{0}'", (object) controllerName));
So this is appears to be by design that an exception is used to return a 404. At least as far as I can tell. In fact System.Web.Mvc.DefaultControllerFactory does exactly the same thing.
Anyways for the most part this answers my question but I'll leave it open a little longer in case someone smarter than me can shed a bit more insight.
Edit: As a solution to logging the error it is simple enough to check the HttpException.GetHttpCode()
value for 404 and ignore the error.
精彩评论