繁体   English   中英

便携式区域具有相同的控制器/文件夹

[英]Portable Areas have Same Controller/Folders

我正在使用MVC 3和MVC Contrib来拥有便携式区域。 问题是,便携式区域具有类似的视图(例如,家庭,用户管理......)。

例如,每个便携式区域都有一个Home视图(Views / Home / Index.cshtml)和一个Home Controller(Controllers / Home.cs)。 设置路由以便访问页面,我将转到MySite / PortableAreaName / Controller / View。

问题是它只加载第一个便携式区域的主页和用户管理页面。 它首先查找视图,然后加载它,而不管URL中指定的区域。

我可以只为所有内容添加前缀(例如,Area1Home,Area1UserManagement),但我宁愿找到更具伸缩性的解决方案。 如果我们有50个以上的便携式区域,每个区域有10个视图,我们要么必须跟踪我们使用的视图名称,要么为每个视图添加前缀。

有什么建议么? 如果您需要更多信息,请告诉我们。

更新1 - 不起作用

使用twang提供的文章并将以下代码添加到主项目(链接文章中的“PortableAreas”)解决。

文章: http//elegantcode.com/2012/04/06/mvc-portable-areas/

主项目> Global.asax.cs

protected void Application_Start()
{
    // ...
    AreaRegistration.RegisterAllAreas();
    PortableAreaRegistration.RegisterEmbeddedViewEngine();
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
    // ...
}

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

    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Login", action = "Index", id = UrlParameter.Optional },
        new string[] { "PortableAreas.Controllers" } // main project namespace
    );
}

便携式区域> MyPortableAreaRegistration.cs

public class MyPortableAreaRegistration : PortableAreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "MyPortableArea";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
    {
        RegisterRoutes(context);
        RegisterAreaEmbeddedResources();
    }

    private void RegisterRoutes(AreaRegistrationContext context)
    {
        context.MapRoute(
            AreaName + "_default",
            base.AreaRoutePrefix + "/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new[] { "MyPortableArea.Controllers", "MvcContrib" }
        );
    }
}

据我了解,以下位置是资源的默认位置:

  • MyPortableArea /内容/图片
  • MyPortableArea /脚本

只要您将资源保存在这些文件夹中,就不应该注册它们。

更新2

之前的修复无效。 即使使用上面的代码,它仍然有时会加载错误的视图,资源等。任何人都知道如何解决这个问题?

当我调试时,它会转到右侧控制器和该控制器内的右侧视图,但会加载错误的视图(例如,它会进入Area2的控制器和Area2的主页方法,但会加载Area1的主页。

例如,我有2个便携区域,但它总是加载第一个

MyPortableArea1 / Controls / Home,MyPortableArea1 / Scripts / Views / Home / index.js MyPortableArea2 / Controls / Home,MyPortableArea2 / Scripts / Views / Home / index.js

我认为这是因为它一次注册所有这些? 然后当它找到路线的匹配时,它使用第一个匹配(有时是错误的)。

更新3

单步执行代码后,我的布局中的部分似乎没有正确加载。 例如,它到达MyPortableArea2的HomeController> Index(),加载布局,然后布局在@RenderBody()部分加载MyPortableArea1的内容。

更新4

http://mvccontrib.codeplex.com/更新到最新版本的MvcContrib。 这没用。

更新5

所以我做了一点测试,弄清楚为什么它加载错误的页面:它根本找不到区域。 这就像它甚至不在系统中!

例如PortableArea1 / Home / Index

public ActionResult Index()
{
    return View("~/Areas/PortableArea1/Views/Home/Index.cshtml");
}

例如PortableArea2 / Home / Index

public ActionResult Index()
{
    return View("~/Areas/PortableArea2/Views/Home/Index.cshtml");
}

当它加载错误的页面时,我现在得到一个错误,说找不到视图。 为什么不加载我的便携式区域?

您可能希望覆盖RegisterAreas(),并为每个区域实现RegisterRoutes()。

这是一篇很好的文章解释: http//elegantcode.com/2012/04/06/mvc-portable-areas/

希望能帮助到你。

这可能是您的Area的_ViewStart.cshtml文件的结果。 它是一个鲜为人知的文件,里面有一个指令:

@{
 Layout = "~/someurl";
}

这将定义为该区域内的每个视图加载基页的位置。 该文件与该区域的web.config文件的范围相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM