MVC网站后台分离

时间:2022-10-23 15:33:27

1、新建一个项目TestAdmin项目类型随便选一个,待会删掉,因为这里我只需要解决方案名称  【项目存放于F盘    F:\TestAdmin

2、新建一个 ZGJ.Web 的前台MVC项目,将开始的那个随便选的项目删掉  【ZGJ.Web地址  F:\TestAdmin\ZGJ.Web

3、新建一个ZGJAdmin 的后台MVC项目,放在 ZGJ.Web 里面 【ZGJAdmin地址 F:\TestAdmin\ZGJ.Web\ZGJAdmin

4、为了美观  将项目 ZGJAdmin  重命名为 ZGJ.Admin 并且在其属性中将程序集名称及默认命名空间名称一起改成 ZGJ.Admin

注意:不要删除 ZGJ.Admin 下的 web.config ,否则添加Controller是会出错   运行所选代码生成器时出错 参数错误 HRESULT: 0x80070057 (E_INVALIDARG)

5、在 ZGJ.Web 下新建一个视图引擎  CustomerRazorViewEngine  继承自 RazorViewEngine

   public class CustomerRazorViewEngine : RazorViewEngine
    {
        public CustomerRazorViewEngine()
        {
            AreaViewLocationFormats = new[]
                                          {
                                              //default
                                              "~/ZGJAdmin/Views/{1}/{0}.cshtml", 
                                              "~/ZGJAdmin/Views/Shared/{0}.cshtml"
                                          };
            AreaMasterLocationFormats = new[]
                                            {
                                                //default
                                                "~/ZGJAdmin/Views/{1}/{0}.cshtml", 
                                                "~/ZGJAdmin/Views/Shared/{0}.cshtml"
                                            };
            ViewLocationFormats = new[]
                                      {
                                            //default
                                            "~/Views/{1}/{0}.cshtml", 
                                            "~/Views/Shared/{0}.cshtml"
                                      };
            MasterLocationFormats = new[]
                                        {
                                            //default
                                            "~/Views/{1}/{0}.cshtml", 
                                            "~/Views/Shared/{0}.cshtml"
                                        };
            AreaPartialViewLocationFormats = AreaViewLocationFormats;
            PartialViewLocationFormats = ViewLocationFormats;
        }
    }

6、在 ZGJ.Web 的 Global.asax 中使网站启动时加载视图引擎

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            ViewEngines.Engines.Clear();
            ViewEngines.Engines.Add(new CustomerRazorViewEngine());
        }
    }

7、修改  ZGJ.Web  中的 RouteConfig  给默认路由加上命名空间

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new[] { "ZGJ.Web.Controllers" }
            );
        }

8、注册区域 在 ZGJ.Admin 中添加类 AdminAreaRegistration  继承自 AreaRegistration

    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "Admin";
            }
        }
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { controller = "Home", action = "Index", area = "Admin", id = "" },
                new[] { "ZGJ.Admin.Controllers" }
            );
        }
    }

9、在 ZGJ.Admin 的 View 目录下 添加 _ViewStart.cshtml 布局文件

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

10、在 ZGJ.Admin 的 View 目录下添加 Shared 目录 并添加_Layout.cshtml 布局文件

11、在 ZGJ.Admin 的属性中修改项目的输出目录

MVC网站后台分离

 发布

1、在ZGJ.Web 中 打包/发布Web 配置 Release 并选中此项目文件夹中的所有文件

MVC网站后台分离

2、编辑解决方案文件  ZGJ.Web..csproj 在最后面添加

  <Target Name="ExcludeRootBinariesDeployment">
    <ItemGroup>
      <RootBinFiles Include="bin\*">
        <InProject>false</InProject>
      </RootBinFiles>
    </ItemGroup>
    <PropertyGroup>
      <ExcludeFilesFromDeployment>
        **\*.Debug.config;
        **\*.Release.config;
        **\obj\**;
        **\bin\*.xml;
        **\*.cs;
        **\packages.config;
        **\*.csproj;
        **\*.csproj.user;
        ZGJAdmin\bin\**;
        ZGJAdmin\db_backups\*.bak;
        Properties\**\*;
      </ExcludeFilesFromDeployment>
    </PropertyGroup>
  </Target>
    <Target Name="ExcludeRootBinariesPackage" DependsOnTargets="ExcludeRootBinariesDeployment" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="$(ExcludeFilesFromDeployment)" />
    </ItemGroup>
  </Target>

MVC网站后台分离