区域不使用正确的视图

时间:2013-12-23 18:28:58

标签: asp.net asp.net-mvc

我已经在我的mvc 4网站上添加了一个管理区域,以便轻松维护我的网站。当我访问mywebsite.com/admin/home时,会调用正确的控制器,但它会进入我错误的主视图。

所以我有一个名为admin的区域,我的主要网站控制器和视图不在某个区域(我不知道是否会导致问题)

我的路线:

        routes.MapRoute(
    name: "Admin_default",
    url: "Admin/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    namespaces: new string[] { "Topsite.Areas.Admin.Controllers" }
 );

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

注意:我有一个自定义视图引擎。

的ViewEngine:

              AreaViewLocationFormats = new[] {

        "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",

        "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",

        "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",

        "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"

    };


    AreaMasterLocationFormats = new[] {

        "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",

        "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",

        "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",

        "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"

    };


    AreaPartialViewLocationFormats = new[] {

        "~/Areas/{2}/Views/%1/{1}/{0}.cshtml",

        "~/Areas/{2}/Views/%1/{1}/{0}.vbhtml",

        "~/Areas/{2}/Views/%1/Shared/{0}.cshtml",

        "~/Areas/{2}/Views/%1/Shared/{0}.vbhtml"

    };



    ViewLocationFormats = new[] {

        "~/Themes/" + Theme  + "/Views/{1}/{0}.cshtml",

        "~/Themes/" + Theme  + "/Views/%1/Shared/{0}.cshtml",

    };



    MasterLocationFormats = new[] {

     "~/Themes/" + Theme  + "/Views/%1/{1}/{0}.cshtml",

    "~/Themes/" + Theme  + "/Views/%1/Shared/{0}.cshtml",


    };


    PartialViewLocationFormats = new[] {

         "~/Themes/" + Theme  + "/Views/%1/{1}/{0}.cshtml",

        "~/Themes/" + Theme  + "/Views/%1/{1}/{0}.vbhtml",

          "~/Themes/" + Theme  + "/Views/%1/Shared/{0}.cshtml",

         "~/Themes/" + Theme  + "/Views/%1/Shared/{0}.vbhtml"

    };

如果有人可以帮助我那会很棒!

如果您还有其他需要,请告诉我。

1 个答案:

答案 0 :(得分:0)

您只需要添加名为:

的文件
_ViewStart.cshtml

在每个区域视图文件夹下,编辑文件以指向根布局,如下所示:

@{
    Layout = "~/Areas/Admin/Shared/Views/_Layout.cshtml";
}