当前位置: 移动技术网 > IT编程>开发语言>.net > 使用Areas分离ASP.NET MVC项目

使用Areas分离ASP.NET MVC项目

2019年04月18日  | 移动技术网IT编程  | 我要评论

国企招聘,医典天术,同志窝

  以前做mes项目遇到过这个情况,一个项目有7到8个大模块,生产、质量、物耗、电子看板、设备等,每个模块都有大量业务,这样使用mvc结构如果所有模块放在一个目录中,那么势必会产生很多问题,各模块代码不好管理,每个模块不能单独发布,这时候如果能够将每个模块单独成一个项目,那么感觉会方便很多。

  可能很多人都想到用area用作分区,但是这个还是在同一个项目中,如果模块较多,controller过多,还是感觉太臃肿。当时公司项目就是每个模块都是一个项目,不过当时是个菜鸟(现在也是),没注意过也没去研究过,后面走了之后,也没遇到过很多模块(一个模块很多业务)的项目,所以一直没有去研究。最近自己想自己做一个通用权限的系统,就想着将这个权限管理通用出来,单独为一个项目,这样做成一个后台开发平台框架。

  网上搜索过“如何将area单独为一个dll”等等。

  找到如下资料:

  asp.net mvc area使用-将area设置成独立项目 : 

    使用areas分离asp.net mvc项目 : 

  自己试着做过,但是感觉脑子不够,没成功。

  正好最近在研究 ,里面有个 razor类库的概念:

     

  瞬间感觉好像就是我在找的东西。

   开始                                                                            

  1.新建 web 应用项目(web.admin)

  

  

  2.添加razor类库(web.authority),记得在web.admin项目中引用该项目。

     

  3. 在web.admin项目中启用 area ,

    在startup类configure,增加红色代码。

 1         public void configure(iapplicationbuilder app, ihostingenvironment env)
 2         {
 3             if (env.isdevelopment())
 4             {
 5                 app.usedeveloperexceptionpage();
 6             }
 7             else
 8             {
 9                 app.useexceptionhandler("/home/error");
10                 // the default hsts value is 30 days. you may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
11                 app.usehsts();
12             }
13 
14             app.usehttpsredirection();
15             app.usestaticfiles();
16             app.usecookiepolicy();
17 
18             app.usemvc(routes =>
19             {
20                 routes.maproute(
21                     name: "default",
22                     template: "{controller=home}/{action=index}/{id?}");
23 
24                 //启用area
25                 routes.maproute(
26                     name: "area",
27                     template: "{area:exists}/{controller=home}/{action=index}/{id?}"
28                 );
29             });
30         }

    4.web.authority项目参照mvc的结构,增加controllers,model,views等目录。

    5.在controllers文件夹中,增加controller,.net core 现在可以直接用 区域属性标记area.

 1  using microsoft.aspnetcore.mvc;
 2  
 3  namespace tiandi.web.authority.controllers
 4  {
 5      [area("sys")]
 6      public class baseusercontroller : controller
 7     {
 8          public actionresult index()
 9         {
10             return view();
11          }
12      }
13  }

 

    6.参照mvc的结构,自己创建视图。在页面index.cshtml右键,属性,设置生成操作为内容,这个非常重要。

                         

 

    7.这样运行项目进行调试,浏览器输入路径 https://localhost:44386/sys/baseuser/index 进行访问,可以看到正常进入controller,但是提示找不到页面。

      

    8.在页面提示中可以明显看到提示,在那些路径中查找没有找到目录。那么可能是view的搜索区域有问题。后面百度搜索了一堆,搜索到了razorviewengineoptions等。

      在startup类configureservices方法增加如下代码:

 1         public void configureservices(iservicecollection services)
 2         {
 3             services.configure<cookiepolicyoptions>(options =>
 4             {
 5                 // this lambda determines whether user consent for non-essential cookies is needed for a given request.
 6                 options.checkconsentneeded = context => true;
 7                 options.minimumsamesitepolicy = samesitemode.none;
 8             });
 9             //用于寻找其他类库的页面
10             services.configure<razorviewengineoptions>(options =>
11             {
12                 options.areaviewlocationformats.clear();
13                 options.areaviewlocationformats.add("/views/{1}/{0}.cshtml");
14             });
15             services.addmvc().setcompatibilityversion(compatibilityversion.version_2_2);
16         }

 

   9、记得步骤6中的页面甚至生成属性。再次运行,可以看到找到类库中的页面。

 大功告成,这样web.authority项目的内容只会生成编译文件,并不会将view的页面复制到web.admin中,发布文件都会少很多。

 如果有多模块分开开发,这样发布的时候只需要将自己模块的编译文件进行发布即可,会方便不少。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网