AddMvc/AddMvcCore/AddControllers等区别

  比较一下AddMvc、AddMvcCore、AddController等在服务配置时的区别及各自拥有的功能。

 

1.services.AddMvcCore()

只注册路由请求和执行控制器所必要的核心服务,确保 Pipeline 程序可运转。除非是有能力并想完全去自主DIY,一般不建议直接使用这个。

 

2.services.AddControllers()

除包含了 AddMvcCore() 所有功能,再加上:

  • Authorization
  • ApiExplorer
  • Data Annotation
  • Formatter Mapping
  • CORS

要用 Controller 但不用View,新建WebAPI时,默认采用的就是这个,使用这个时,与SwashBuckle配合时,无需再额外引入ApiExplorer,自身已经依赖。

 

3.services.AddRazorPages()

包含 AddMvcCore() 所有功能,再加上:

  • Razor Pages
  • Authorization
  • Data Annotation
  • Cache Tag Helper

 

4.services.AddControllersWithViews()

包含 AddControllers() 所有功能,再加上:

  • cshtml和Razor View
  • Cache Tag Helper

标准MVC模式,常用Razor视图,使用这个就够了

 

5.services.AddMvc()

包含 AddControllersWithViews() 及 AddRazorPages() 功能。 包含的功能最为齐全,如果不想遗漏功能,直接使用这个就行

 

 

2020-09-05,望技术有成后能回来看见自己的脚步 
posted @ 2020-09-05 23:03  微笑刺客D  阅读(2916)  评论(1编辑  收藏  举报
返回顶部