0

我已经用 durandal 介绍了 mvc 区域概念

我的层次结构是

Area.Web
    Areas
        Blog
           Controller
                 SocialController
                   Social
           View
        Task
           Controller
           View
    Scripts
        App
          Blog
            ViewModels

我有根据我的网址到该区域的路线。例如,本地主机/博客

我的路线将是:

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

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

当我尝试导航到 localhost/blog 时,它会调用正确的控制器并正确呈现。当 durandal 路由发生时博客被排除。所以我的路由 url 无法获取控制器。

 app.setRoot('viewmodels/social/home', 'entrance');

以下路由抛出 404 异常,因为它忽略了路由 url (localhost/social/home) 中的博客

请告诉我,如何解决这个问题。是否可以在所有路线和 app.set 中包含区域名称

4

1 回答 1

1

首先恕我直言,您应该重新考虑您的项目架构。例如,如果您需要使用 Weyland 优化此应用程序,则两个 SPA 将最终在同一个文件中,这不会为您提供所需的分离。

但是,您可以创建单独的文件夹,并为每个 SPA 提供它自己的 main.js 文件,而不是将它们都放在 app 文件夹下。

在这种情况下,您将具有以下项目结构:

Area.Web
    AppBlog
        Services
        Views
        ViewModels
        main.js
    AppTask
        Services
        Views
        ViewModels
        main.js 
    Areas
        Blog
           Controller
           View
        Task
           Controller
           View
    Scripts
       durandal
       etc.
    Content
    etc.

您在博客区域中的视图将使用以下 JS 导入:

<script src="~/Scripts/require.js" data-main="@Url.Content("~/AppBlog/main")"></script>

而任务区将使用

<script src="~/Scripts/require.js" data-main="@Url.Content("~/AppBlog/main")"></script>

在这样的设置中,您的路线看起来与您的代码中的路线完全相同

编辑

routes.MapRoute(
name: "blog",
url: "blog/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional },
                        namespaces: new string[] { "Area.Web.Blog.Controllers" }
                    );

routes.MapRoute(
    name: "Task",
    url: "Task/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional},
    namespaces: new string[] { "Area.Web.Task.Controllers" }

);

于 2014-01-16T13:12:59.490 回答