使用区域,可以有效的对业务进行隔离,各种业务及分工可以更灵活。在Asp.Net Core中启用区域也是极简单的。

使用步骤:

1、在 Startup.cs 中添加区域的路由:

            app.UseMvc(routes =>
{
routes.MapRoute(
name: "area",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"); routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});

2、在项目下面创建 Areas 文件夹,并添加相应的 Controllers, Views, Models 文件夹:

3、在 Controllers 下添加 HomeController.cs ,(通常我会建议,添加实际的 Controller 之前,先创建 BaseController ,并且其它所有Controller全部继承自 BaseController,这样在实际开发过程中,很多配置及常用方法可以更容易使用),并在Controller上应用属性 [Area("名称")],若在 BaseController 中进行设置。则继承自 BaseController 的所有 Controller 都无需重复设置:

    [Area("Mobile")]
public class BaseController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
}

4、后端重定向的时候,需要加上Area属性:

        public IActionResult Test()
{
return RedirectToAction("index", "home", new { Area = "mobile" });
}

5、前端链接生成也需要加上 Area 属性:

@{
ViewData["Title"] = "About";
}
<h2>@ViewData["Title"].</h2>
<h3>@ViewData["Message"]</h3> <p>Use this area to provide additional information.</p> <a asp-area="mobile" asp-controller="home" asp-action="index">Test</a>

OK,其它一些使用时注意事项,同以前的Asp.Net几乎一样。

最新文章

  1. 十分钟轻松让你认识Entity Framework 7
  2. Ubuntu中配置Thunderbird登录qq邮箱
  3. python time函数
  4. Bootstrap-模态框Modal使用
  5. 几款命令行工具(CMD)增强软件
  6. Nginx 配置指令的执行顺序(六)
  7. css中,如何设置前景色的透明度?
  8. (六十二)纯代码搭建UI
  9. 环境连接报错(最大连接数超过) APP-FND-01516
  10. [Kubernetes]资源模型与资源管理
  11. VUE 多页面配置(二)
  12. CF797F Mice and Holes 贪心、栈维护DP
  13. Python语言知识总结
  14. 网页常见单位: px em pt % rem vw、vh、vmin、vmax , rem 使用
  15. python学习之老男孩python全栈第九期_day003知识点总结
  16. VS2010-自定义控件
  17. Lua脚本语法说明(转):
  18. 机器学习(5): K-means 算法
  19. python学习-day 1
  20. ubuntu 12.04下apache 配置家目录地址

热门文章

  1. 那晚征服的一道js经典的面试题
  2. WebApi传参总动员(五)
  3. DIV嵌套垂直居中
  4. csharp:Compare two DataTables to rows in one but not the other
  5. JavaScript调Java
  6. javascript --- 将共享属性迁移到原型中去
  7. SharePoint 2013 自定义模板页后在列表里修改不了视图
  8. Ubuntu上安装Apache、MySql和PHP
  9. mac java 环境设置
  10. JAVA基础学习day19--IO流一、FileWrite与FileReader