ASP.NET Core MVC中添加区域Area的简单实现

ASP.NET Core中的Area注册在Startup.cs中完成。

1.在VS中通过右键点击添加区域,输入区域名即可,如添加区域“Admin”,便会生成如下文件。

ASP.NET Core MVC中添加区域Area的简单实现_第1张图片

2.在Startup.cs中添加区域路由:

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

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

3.在Controller上应用属性 [Area("名称")],我这里是[Area("Admin")]。

ASP.NET Core MVC中添加区域Area的简单实现_第2张图片

4.RedirectToAction重定向时,需要加上Area属性

如:RedirectToAction("Index", "Home", new { Area = "Admin" });

这样便会跳到http://你的域名/Admin/Home/Index ,不然会是这样:http://你的域名/Home/Index?Area=Admin。

你可能感兴趣的:(Asp.Net,Core)