How do i prevent System.NotSupportedException: The given path's format is not supported
When a bad url is used, and all chars are accepted - either using registry of .config keys (see other "duplicates" posts for more info), asp.net mvc crashes.
What is the best interception point to p开发者_如何学编程revent/handle the exception ?
Sample urls
http://www.local.com/some/url/pagehttp://www.local.com/some/url/page
http://www.local.com/some/url/page:12
Sample exception
System.NotSupportedException The given path's format is not supported.
at System.Security.Util.StringExpressionSet.CanonicalizePath(String path, Boolean needFullPath)
at System.Security.Util.StringExpressionSet.CreateListFromExpressions(String[] str, Boolean needFullPath)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path)
at System.Web.HttpRequest.MapPath(VirtualPath virtualPath, VirtualPath baseVirtualDir, Boolean allowCrossAppMapping)
at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage)
at System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm)
at System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)
at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1c.<InvokeActionResultWithFilters>b__19()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
Fixed.
Add this to your Application_Start:
filters.Add(new MyHandleErrorAttribute { Order = 1, ExceptionType = typeof(NotSupportedException) });
And put this class into your project:
public class MyHandleErrorAttribute : System.Web.Mvc.HandleErrorAttribute {
public override void OnException(ExceptionContext filterContext) {
if (filterContext.IsChildAction || filterContext.ExceptionHandled || !ExceptionType.IsInstanceOfType(exception) || exception.Message != "The given path's format is not supported." )
return;
}
base.OnException(filterContext);
filterContext.HttpContext.Response.StatusCode = 404;
}
Then you can handle the 404 in your Application_Error.
精彩评论