【问题标题】:ASP.NET MVC 5 Routing configuration not checking assigned folder?ASP.NET MVC 5 路由配置不检查分配的文件夹?
【发布时间】:2017-01-04 20:18:00
【问题描述】:

我在使用 ASP 路由引擎时遇到了一些问题,这些问题不言自明。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "GameGold",
        url: "Products/GameGold/{controller}/{action}/{id}",
        defaults: new { controller = "Coins", action = "Index", id = UrlParameter.Optional }
        );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

当我转到 URL localhost/Products/GameGold/Coins/ 时,就会出现这个。

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Coins/Index.aspx
~/Views/Coins/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Coins/Index.cshtml
~/Views/Coins/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml

我的文件夹是这样布置的

/Views/Products/GameGold/Coins/Index.cshtml

【问题讨论】:

  • 请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标记。

标签: c# asp.net-mvc asp.net-mvc-5 routing


【解决方案1】:

没有找到你的“Index”视图的错误不是路由定义造成的。

URL localhost/Products/GameGold/Coins/ 被“GameGold”路由映射到CoinsController 及其Indexaction。

MVC 的内置约定是在 ~/Views/[CONTROLLERNAME] 文件夹中查找视图 - 所以它在 ~/Views/Coins/ 文件夹中查找。

要解决此问题,您有两种选择:

1.) 遵守约定,将/Views/Products/GameGold/Coins/Index.cshtml 移至/Views/Coins/Index.cshtml

2.) 更改 Razor 引擎的 ViewLocationFormats 以适应您的目录布局。您可以在 http://www.ryadel.com/en/asp-net-mvc-add-custom-locations-to-the-view-engine-default-search-patterns/ 等博客文章中找到详细信息 这篇文章的一个例子:

// Add /MyVeryOwn/ folder to the default location scheme for STANDARD Views
var razorEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault();
razorEngine.ViewLocationFormats = 
    razorEngine.ViewLocationFormats.Concat(new string[] { 
        "~/Views/Products/GameGold/{1}/{0}.cshtml",
        "~/Views/Products/GameGold/{0}.cshtml"
        // add other folders here (if any)
    }).ToArray();

【讨论】:

    猜你喜欢
    • 2015-12-09
    • 2017-07-12
    • 2021-04-23
    • 1970-01-01
    • 2016-01-07
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 2014-12-01
    相关资源
    最近更新 更多