MVC控制器下的MVC Web应用程序项目

Pat*_*ick 2 c# iis asp.net-mvc routing nopcommerce

我有两个区别MVC Web应用程序访问:

1) product.main-brand.com        (solution landing page)
2) admin.product.main-brand.com  (solution admin landing page)
Run Code Online (Sandbox Code Playgroud)

该产品将改变位置:

product.main-brand.com to www.main-brand-com/product
Run Code Online (Sandbox Code Playgroud)

管理员必须改变:

admin.product.main-brand.com to www.main-brand-com/product/admin
Run Code Online (Sandbox Code Playgroud)

我无法为管理员创建虚拟目录,因为www.main-brand-com/product是一个控制器.

例如,NopCommerce这样做,我们可以从www.shop.com转到www.shop.com/admin,它正在改变项目,而不是控制器/行动.他是怎么做到的?

Rap*_*ael 5

以下是实现类似于nopCommerce 3.9的类似解决方案的一些步骤

创建主Web项目

Foo.Web

  • 属性
    • AssemblyInfo.cs中
  • Foo.Web.csproj
  • Global.asax中
  • 的Global.asax.cs
  • ...

在主Web项目中创建管理Web项目

Foo.Web

  • 管理
    • 属性
      • AssemblyInfo.cs中
    • Foo.Admin.csproj
    • ...

必须使用"添加项目向导"或使用资源管理器创建管理文件夹.不要使用解决方案资源管理器创建该文件夹.

从管理项目中删除Global.asax

你不需要这个

将AreaRegistration实现添加到您的管理项目

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName => "Admin";
    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute
        (
            name: "AdminDefault",
            url: "admin/{controller}/{action}/{id}",
            defaults: new {controller = "Home", action = "Index", area = "admin", id = ""},
            namespaces: new[] {"Foo.Admin.Controllers"}
        );
    }
}
Run Code Online (Sandbox Code Playgroud)

修改Global.asax.cs以注册区域

将其添加到您的项目中.确保在默认路由之前调用它.

AreaRegistration.RegisterAllAreas();
Run Code Online (Sandbox Code Playgroud)

这样,Foo.Admin中的所有控制器都可以通过〜/ Admin/{controller}/{action}找到,而Foo.Web中的所有控制器都可以通过〜/ {controller}/{action}找到

  • TLDR:了解MVC领域.它们允许您将应用程序分解为更小的部分.无论何时创建新区域,只需在应用程序中注册其路径即可.(更多文档:https://msdn.microsoft.com/en-us/library/ee671793%28v=vs.100%29.aspx) (2认同)