• MVC网站后台分离


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

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

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

    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 的属性中修改项目的输出目录

     发布

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

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

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

  • 相关阅读:
    tabhost切换标签:Log中出现You must supply a layout_width attribute的解决方法
    listview去掉底部多出的边框黑色
    使用fragmenttabhost后,子fragment怎么获取ID?怎么用getSharedPreferences
    android用shape给linearLayout设置边框,怎样只保留底部或顶部的边框,把其它三个方向的边框去掉呢?
    linux删除文件未释放空间问题处理
    mount: unknown filesystem type 'LVM2_member'解决方案【转】
    centos系统lvm的安装
    一个或多个音频服务未运行 win7 错误1079:此服务的账户不同于运行于同一进程上的其他服务账户
    php SimpleXML
    new JSONObject(str)无法解析 报错:org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
  • 原文地址:https://www.cnblogs.com/ideacore/p/6289034.html
Copyright © 2020-2023  润新知