0

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

4

3 回答 3

0

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

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

于 2013-05-29T19:56:47.847 回答
-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"
    };
}

网络表单:

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"
    };
}
于 2013-05-29T19:59:44.377 回答
-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

于 2013-05-29T20:31:45.710 回答