.NET MVC 4无法正确定位视图文件夹

时间:2013-05-29 19:51:52

标签: asp.net-mvc

我有一个MVC 4站点,在Views文件夹下我有各种视图的文件夹。其中一个是名称“服务”。在控制器下我有服务控制器。一切都在匆匆忙忙。然后,我在站点的根目录中创建了一个名为“Services”的文件夹。现在,当路由使用该文件夹来查找视图而不是在Views文件夹中查找时。 ????任何人都可以让我洞察这个现象吗?我只是重命名了第二个文件夹,生活又恢复了,但这是意料之外的,因为我认为默认情况下路由进入了views文件夹。

3 个答案:

答案 0 :(得分:0)

根本不应该有Services个文件夹。这告诉路由忽略而不是调用ASP.NET MVC。

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

答案 1 :(得分:-1)

你的路由很可能是/ 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"
    };
}

的WebForms:

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

答案 2 :(得分:-1)

您可以在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