首页 文章

具有特殊字符和子操作的MVC路由参数

提问于
浏览
5

我有以下路线,它需要能够处理其中任何角色的游戏 . 我在web.config中正确启用了一些东西以允许URL中的特殊字符,并确认我的URL在使用基本查询字符串而不是自定义MVC路由时有效 . 但是,只要我通过Html.Action包含子操作加载,我就会得到以下堆栈跟踪 .

routes.MapRoute("Game", "Game/Game/{id}", new { controller = "Game", action = "Game", id = ""}, new { id = @"[^\.]*" });

这是我的Controller方法和视图(我把它们弄了很多,但错误仍然存在),它被路由调用,甚至视图内部的剃刀也会被调用,但后来我得到了下面的错误/堆栈跟踪 .

控制器:

public ActionResult Game(string id)
  {
     {
        Game currentGame = _ugdb.Games.FirstOrDefault(g => g.GameName.Equals(id));
        return View(currentGame);

  }

视图:

@model UltimateGameDB.Domain.Entities.Game
@Html.Action("SearchBar", "Search")
<span>@Model.GameName</span>

子动作控制器方法(不被调用):

[ChildActionOnly]
  public ActionResult SearchBar()
  {
     ViewBag.TotalGames = _ugdb.Game_Platform.Count() + _ugdb.Games.Count(g => g.Game_Platform.Count == 0);
     return PartialView("_SearchBar");
  }

以下是一些不起作用的路线示例:

http://localhost:58386/Game/Game/Sid%20Meier%27s%20Civilization%3a%20Beyond%20Earth

http://localhost:58386/Game/Game/Starcraft%20II%3a%20Heart%20of%20the%20Swarm

虽然我的数据库中基本上任何具有特殊字符的游戏都会出现同样的错误 . 实际的错误消息如下:

System.NotSupportedException:不支持给定路径的格式 . 在System.Security.Peron上的System.Security.Util.Sumissions.SpateA.Perate中的System.Security.Util.Spress.PreateListFromExpressions(String [] str,Boolean needFullPath)处的System.Security.Util.StringExpressionSet.CanonicalizePath(String path,Boolean needFullPath)处于FileIOPermissionAccess访问权限(FileIOPermissionAccess访问, AccessControlActions控件,String [] pathListOrig,布尔checkForDuplicates,Boolean needFullPath,Boolean copyPathList)System.Security.FerI上的System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess访问,字符串路径),System.Web.InternalSecurityPermissions.PathDiscovery(字符串路径) . System.Web.HttpRequest.MapPath(VirtualPath virtualPath)上的System.Web.HttpServer.tilityPath(VirtualPath virtualPath)中的Web.HttpRequest.MapPath(VirtualPath virtualPath,VirtualPath baseVirtualDir,Boolean allowCrossAppMapping),系统处于System.Web.HttpServerUtility.Execute(IHttpHandler处理程序,TextWriter writer,Boolean preserveForm,Boolean setPreviousPage)系统中的.Web.HttpServerUtility.Execute(IHttpHandler处理程序,TextWriter编写器,布尔保留形式) System.Web上的System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper,String actionName,String controllerName,RouteValueDictionary routeValues,TextWriter textWriter)中的m.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler,TextWriter writer,Boolean preserveForm) . ASP._Page_Views_Game_Game_cshtml.Execute()上的System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,String actionName,Object routeValues)中的Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper,String actionName,String controllerName,RouteValueDictionary routeValues)在d:\ TFS Workspace \ UltimateGameDB \ Dev \ Source \ ULTIMATEGAMEDB \ UltimateGameDB.WebUI \ Views \ Game \ Game.cshtml:System.Web.Mvc.WebViewPage的System.Web.WebPages.WebPageBase.ExecutePageHierarchy()中的第257行 . 在System.Web.WebPages.Web中的System.Web.WebPages.StartPage.ExecutePageHierarchy()的System.Web.WebPages.StartPage.RunPage()处的ExecutePageHierarchy()(WebPageContext pageContext, System.Web.Mvc上System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer)的System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object instance)中的TextWriter writer,WebPageRenderingBase startPage . 位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive的System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)中的ViewResultBase.ExecuteResult(ControllerContext context)(IList1过滤器,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult)在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1过滤器,Int32 filterIndex,ResultExecutingContext preContext,ControllerContext controllerContext,ActionResult actionResult)在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList1过滤器,Int32 filterIndex,ResultExecutingContext p) System.Web.Mvc.Async.AsyncControllerActionInvoker中的System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext,IList1过滤器,ActionResult actionResult)中的reContext,ControllerContext controllerContext,ActionResult actionResult . <> c__DisplayClass21 . <> c__DisplayClass2b.b__1c()在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1上System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.CallEndDelegate(IAsyncResult asyncResult)的System.Web.Mvc.Async.AsyncControllerActionInvoker . <> c__DisplayClass21.b__1e(IAsyncResult asyncResult) . 在System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult,ExecuteCoreState innerState)的System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)的End()处于System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate (IAsyncResult的asyncResult)在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()在System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult)在System.Web.Mvc.Controller.b__15(IAsyncResult的asyncResult,控制器控制器)在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult的asyncResult)在System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()在System.Web.Mvc.Controller . EndExecute(IAsyncResult的asyncResult)在System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult的asyncResult)在System.Web.Mvc.MvcHandler.b__5(IAsyncResult的asyncResult,ProcessRequestState innerState)在的System.Web System.Web.Mvc上System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)的System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase1.End()处的.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) .MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的结果)在System.Web.HttpApplication.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult的AR)

我用google搜索了一堆,似乎无法弄清楚为什么这条路线不起作用 .

提前致谢!

1 回答

  • 5

    尝试在 httpRuntime 元素中设置 relaxedUrlToFileSystemMapping="true" . 使用您已添加的属性,它可能看起来像这样:

    <httpRuntime targetFramework="4.5" 
      requestPathInvalidCharacters="" 
      requestValidationMode="2.0" 
      relaxedUrlToFileSystemMapping="true" />
    

    来自HttpRuntimeSection.RelaxedUrlToFileSystemMapping属性的文档:

    获取或设置一个值,该值指示HTTP请求中的URL是否必须是有效的Windows文件路径 .

    在ASP.NET运行时深处的某个地方,它尝试在处理请求之前将请求的URL转换为物理路径 . 如果物理路径无效,此设置似乎可以防止它爆炸 .

相关问题