2011-05-17 119 views
1

當使用錯誤的URL並且接受所有字符時 - 使用.config鍵註冊表(請參閱其他「重複」帖子以獲取更多信息), asp.net mvc崩潰。我該如何防止System.NotSupportedException:不支持給定的路徑格式

防止/處理異常的最佳攔截點是什麼?

樣本網址

http://www.local.com/some/url/pagehttp://www.local.com/some/url/page 
http://www.local.com/some/url/page:12 

樣品異常

System.NotSupportedException 不支持給定路徑的格式。

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) 

回答

1

固定。

添加到您的Application_Start:

filters.Add(new MyHandleErrorAttribute { Order = 1, ExceptionType = typeof(NotSupportedException) }); 

,並把這個類到您的項目:

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; 
} 

然後你就可以處理您的Application_Error 404。

相關問題