本文转自:http://stackoverflow.com/questions/36535511/how-to-use-an-area-in-asp-net-core

Q:

How does one use an Area in ASP.NET Core? This is not properly documented!

Supposing I have an app that needs an Admin section. This section requires its Views to be places in that area. All requests that start with Admin/ will need to be redirected to that area.

A:

In order to include an Area in an ASP.NET Core app, first we need to include a conventional route in the Startup.cs file (It's best to place it before any non-area route):

In Startup.cs/Configure method:

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

Then make a folder named Areas in the app root and make another named Admin inside the former, also make these folders inside Admin (ViewComponent is optional):

Now we create a controller inside the Controllers folder named AdminController, the content can be like:

[Area("Admin")]
[Route("admin")]
public class AdminController : Controller
{
public AdminController()
{
// do stuff
} public IActionResult Index()
{
return View();
} [Route("[action]/{page:int?}")]
public IActionResult Orders()
{
return View();
} [Route("[action]")]
public IActionResult Shop()
{
return View();
} [Route("[action]/newest")]
public IActionResult Payments()
{
return View();
}
}

Now in order for that to work, you'll need to create Views for all actions that return one. The hierarchy for views is just like what you have in a non-area Views folder:

Now, you should be good to go!

Question: What if I what to have another controller inside my Area?

Answer:

Just add another controller beside AdminController and make sure the routes are like the following:

[Area("Admin")]
[Route("admin/[controller]")]
public class ProductsController : Controller
{
public ProductsController()
{
//
} [Route("{page:int?}")]
public IActionResult Index()
{
return View();
}
}

The important part is [Route("admin/[controller]")]. With that you can keep the style of routing to admin/controller/action/...

[转]How to use an Area in ASP.NET Core的更多相关文章

  1. ASP.NET Core MVC 之区域(Area)

    区域(Area)是一个 ASP.NET MVC 功能,用于将相关功能组织为一个单独的命名空间(用于路由)和文件结构(用于视图).使用区域通过向控制器和操作添加 一个路由参数(area)来创建用于路由目 ...

  2. Asp.net core 学习笔记 ( Area and Feature folder structure 文件结构 )

    2017-09-22 refer : https://msdn.microsoft.com/en-us/magazine/mt763233.aspx?f=255&MSPPError=-2147 ...

  3. ASP.NET Core 中文文档 第四章 MVC(4.2)控制器操作的路由

    原文:Routing to Controller Actions 作者:Ryan Nowak.Rick Anderson 翻译:娄宇(Lyrics) 校对:何镇汐.姚阿勇(Dr.Yao) ASP.NE ...

  4. ASP.NET Core 中文文档 第四章 MVC(4.6)Areas(区域)

    原文:Areas 作者:Dhananjay Kumar 和 Rick Anderson 翻译:耿晓亮(Blue) 校对:许登洋(Seay) Areas 是 ASP.NET MVC 用来将相关功能组织成 ...

  5. ASP.NET Core 中文文档 第三章 原理(6)全球化与本地化

    原文:Globalization and localization 作者:Rick Anderson.Damien Bowden.Bart Calixto.Nadeem Afana 翻译:谢炀(Kil ...

  6. ASP.NET Core 中文文档 第四章 MVC(3.1)视图概述

    原文:Views Overview 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:高嵩(Jack) ASP.NET MVC Core 的控制器可以利用 视图 返回格式化结果. 什么 ...

  7. ASP.NET Core 中文文档 第四章 MVC(3.6.2 )自定义标签辅助类(Tag Helpers)

    原文:Authoring Tag Helpers 作者:Rick Anderson 翻译:张海龙(jiechen) 校对:许登洋(Seay) 示例代码查看与下载 从 Tag Helper 讲起 本篇教 ...

  8. 【无私分享:ASP.NET CORE 项目实战(第九章)】创建区域Areas,添加TagHelper

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在Asp.net Core VS2015中,我们发现还有很多不太简便的地方,比如右击添加视图,转到试图页等功能图不见了,虽然我 ...

  9. [转]Asp.Net Core 简单的使用加密的Cookie保存用户状态

    本文转自:http://www.cnblogs.com/Joes/p/6023820.html 在以前的Asp.Net中可以用 FormsAuthentication 类的一系列方法来使用加密的Coo ...

随机推荐

  1. python学习之路 四 :文件处理

    本节重点 掌握文件的读.写.修改方法 掌握文件的处理模式的区别 一.文件读取 ​    ​1.读取全部内容 # 一次性读取文件 f = open("test.txt",'r',en ...

  2. c++实验3 链式存储线性表

    1.线性表链式存储结构及基本操作算法实现 (1)单链表存储结构类的定义: #include <iostream> using namespace std; template <cla ...

  3. docker运行nginx

    docker run --name nginx(容器名) -p 80:80 -v 项目文件路径:/usr/share/nginx/html -v 配置文件路径:/etc/nginx/nginx.con ...

  4. VMware安装linux系统报错:已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作。

    检测问题所在: 下载LeoMoon CPU-V 检查一下CPU VT-x状态是否启用 地址:http://download.csdn.net/detail/qq_22860341/9858011 如果 ...

  5. Centos6和7的区别

    1.init系统 Linux 操作系统的启动首先从 BIOS 开始,接下来进入 boot loader,由 bootloader 载入内核,进行内核初始化.内核初始化的最后一步就是启动 pid 为 1 ...

  6. 手动博客重定向 https://www.cnblogs.com/kelthuzadx/

    https://www.cnblogs.com/kelthuzadx/ 博客狂魔又㕛叒换地址了

  7. IOS开发之——objectForKey与valueForKey在NSDictionary中的差异

    从 NSDictionary 取值的时候有两个方法,objectForKey: 和 valueForKey:,这两个方法具体有什么不同呢? 先从 NSDictionary 文档中来看这两个方法的定义: ...

  8. VMware安装黑威联通教程+文件 亲身测试成功 老骥伏枥黑威联通母盘QNAP1G-BOOT 1G

    1.首先致敬老骥伏枥大神的帖子. [老骥伏枥-原创]制作黑威联通启动盘:进阶篇 2.其次感谢这位作者的安装教程 作者:f541883216 [老骥伏枥-原创]用我的黑威联通启动盘在WMware搭建系统 ...

  9. python学习,day3:文件的修改

    对文件进行修改,对不起,在原文修改比较难,只能采用循环覆盖的方法 # coding=utf-8 # Author: RyAn Bi f = open('yesterday2','r',encoding ...

  10. 使用pageHelper遇到的问题

    在做SSM整合的时候,遇到一个小问题,在我使用pageHelper的时候,分页的效果总是无法正确显示,卡了我几个小时,现在来说一下我的问题.  1.首先导入pageHelper的包: <!--引 ...