【问题标题】:.NET MVC 4 Doesn't Locate view folder correctly.NET MVC 4 无法正确定位视图文件夹
【发布时间】:2013-05-29 19:51:51
【问题描述】:

我有一个 MVC 4 站点,在 Views 文件夹下我有用于各种视图的文件夹。其中之一是名称“服务”。在控制器下,我有服务控制器。一切进展顺利。然后,我在站点的根目录中创建了一个名为“Services”的文件夹。现在,当路由使用该文件夹来查找视图而不是在 Views 文件夹中查找时。 ???谁能让我深入了解这个现象?我只是重命名了第二个文件夹,生活又好起来了,但这是出乎意料的,因为我认为路由默认会转到 views 文件夹。

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您不应该在根目录下有 Services 文件夹。这告诉路由忽略而不调用 ASP.NET MVC。

    您应该有一个Views/Services 文件夹。

    【讨论】:

    • 我相信你是不正确的 - 这取决于 RouteCollection.RouteExistingFiles 在 Global.asax.cs 文件中是否设置为 true 以及它们的路由是否与文件路径完全匹配。
    【解决方案2】:

    很可能您的路由类似于 /Services/MethodName 并且您的视图文件夹也是 /Services/MethodName.ascx 或磁盘上的任何内容...如果您不告诉 MVC 路由现有文件,它会让默认情况下,它们是从磁盘上提供的。要覆盖它,请在 Application_Start 上的 Global.asax.cs 中设置 RouteCollection.RouteExistingFiles = true

    这是从 MVC3 的默认视图引擎中查找视图的反汇编代码:

    剃须刀:

    public RazorViewEngine(IViewPageActivator viewPageActivator) : base(viewPageActivator)
    {
        base.AreaViewLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.vbhtml"
        };
        base.AreaMasterLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.vbhtml"
        };
        base.AreaPartialViewLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.cshtml",
            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
            "~/Areas/{2}/Views/Shared/{0}.cshtml",
            "~/Areas/{2}/Views/Shared/{0}.vbhtml"
        };
        base.ViewLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };
        base.MasterLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };
        base.PartialViewLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/{1}/{0}.vbhtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Shared/{0}.vbhtml"
        };
        base.FileExtensions = new string[]
        {
            "cshtml",
            "vbhtml"
        };
    }
    

    网络表单:

    public WebFormViewEngine(IViewPageActivator viewPageActivator) : base(viewPageActivator)
    {
        base.MasterLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.master",
            "~/Views/Shared/{0}.master"
        };
        base.AreaMasterLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.master",
            "~/Areas/{2}/Views/Shared/{0}.master"
        };
        base.ViewLocationFormats = new string[]
        {
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx"
        };
        base.AreaViewLocationFormats = new string[]
        {
            "~/Areas/{2}/Views/{1}/{0}.aspx",
            "~/Areas/{2}/Views/{1}/{0}.ascx",
            "~/Areas/{2}/Views/Shared/{0}.aspx",
            "~/Areas/{2}/Views/Shared/{0}.ascx"
        };
        base.PartialViewLocationFormats = base.ViewLocationFormats;
        base.AreaPartialViewLocationFormats = base.AreaViewLocationFormats;
        base.FileExtensions = new string[]
        {
            "aspx",
            "ascx",
            "master"
        };
    }
    

    【讨论】:

    • 感谢您的意见。我没有时间进一步调查。当我添加文件夹时,我确实在其中有一个方法类,但它不是控制器或视图。令我惊讶的是,MVC 默认使用根目录下的 Services 文件夹并跳过 Views 中的 Services 文件夹。
    【解决方案3】:

    您可以在 RouteConfig 文件中的 App_Start 文件夹中配置路由。如果你添加

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

    在 routes.IgnoreRoute 行之外,您将能够在 MVC 项目中拥有 Services 文件夹,以及 Services 控制器及其视图。

    如果您现在在其他视图之一中有类似的内容:

    @Html.ActionLink("To services", "Index", "Services")
    

    它现在将呈现为 ~/Services/Index 而不仅仅是 ~/Services

    【讨论】:

    • 这没有回答问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多