- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}"
- );
默认访问可以匹配到
https://localhost:44302/home/index/1
https://localhost:44302/home/index
https://localhost:44302/home
https://localhost:44302
也可以匹配到
https://localhost:44302/course/learn/1
Controllers文件夹下右键-,添加控制器->
-》CourseController.cs
->
在Views文件夹下,新建 Course 文件夹,然后右键 。 添加-》视图-》learn.cshtml
- @{
- ViewData["Title"]= "学习";
- }
-
学习页面内容
- //防止输入https://localhost:44302/home跳到博客专用路由
- routes.MapRoute(
- name: "home",
- template: "home", //https://localhost:44302/home,跳到Home控制器中Index方法
- defaults: new { controller = "Home", action = "Index" }
- );
-
- //博客专用路由
- routes.MapRoute(
- name:"blog",
- template:"{name}", //https://localhost:44302/博客名,跳到Blog控制器中Index方法
- defaults: new { controller ="Blog",action="Index"}
- );
- public class BlogController : Controller
- {
- public IActionResult Index(string name)
- {
- ViewData["BlogName"] = name;
- return View();
- }
- }
@ViewData["BlogName"]博客
特性路由:在Action添加标注,来指定路由规则
1、Startup.cs 文件中 app.UseMvc(删除全部路由);
2、在Action添加标注
- public class HomeController : Controller
- {
- [Route("")]
- [Route("home")]
- [Route("home/index")]
- public IActionResult Index()
- {
- return View();
- }
- [Route("home/abc")]//https://localhost:44302/home/abc
- public IActionResult About()
- {
- ViewData["Message"] = "Your application description page.";
-
- return View();
- }
- }
注意:
A、如果2个方法有相同标注,为防止冲突,可以这样做
[HttpPost("home/abc")]
B、特性路由参数
- //学习
- [Route("course/learn/{id}")] //https://localhost:44302/course/learn/1
- public IActionResult learn(int id)
- {
- return View();
- }
组合路由:在 Controller 和Action 上添加路由标注
- //课程
- [Route("c")]
- public class CourseController : Controller
- {
-
- [Route("index")] // https://localhost:44302/c/index
- [Route("/idd")] // 以/开头的不是组合路由 https://localhost:44302/idd
- public IActionResult Index()
- {
- return View();
- }
- }
在路由模板中使用
[controller] 表示和控制器同名的名称
[action] 表示和Action同名的名称
[area] 表示和区域同名的名称
- [Route("[controller]")]
- public class CourseController : Controller
- {
- [Route("[action]")] // https://localhost:44302/course/index
- public IActionResult Index()
- {
- return View();
- }
- }
还可以在控制器上面使用多个路由特性,实现多个地址匹配一个控制器
- [Route("[controller]")]
- [Route("more")] // https://localhost:44302/more/index
- public class CourseController : Controller
- {
- [Route("[action]")] // https://localhost:44302/course/index
- public IActionResult Index()
- {
- return View();
- }
- }
还可以这样写
- [Route("[controller]/[action]")] //https://localhost:44302/course/index
- //也可以放到方法上
- public class CourseController : Controller
- {
- public IActionResult Index()
- {
- return View();
- }
- }
通过创建一个实现了IRouteTemplateProvider的类。然后在类里面配置Template和Order还有Name这三个属性就可以实现特性标注了。
只要那个类或者Action增加了这个自定义标注就可以自动设置上面的Template特性。
- namespace FirstCoreMvc.core
- {
- public class MyRouteAttribute : Attribute, IRouteTemplateProvider
- {
- public string Template => "www/[controller]/[action]";
-
- public int? Order {get;set;}
-
- public string Name { get; set; }
- }
- }
2、调用
- public class BlogController : Controller
- {
- [MyRoute] //https://localhost:44302/www/blog/index
- public IActionResult Index(string name)
- {
- ViewData["BlogName"] = name;
- return View();
- }
- }
生成url,实际应用在视图中生成链接的地址
- public IActionResult Index()
- {
-
- //return Content(Url.Action("index")); // /home
- //return Content(Url.Action("index", "home")); // /home
-
- return Content(Url.Action("index", "course", new { id = 1, name = "lili" }));
- // /course/index/1?name=lili 和我想像/course/index?id=1&1name=lili不一样,有人知道吗?
-
- }
- namespace FirstCoreMvc.Areas.Manage.Controllers
- {
- [Area("Manage")] //需要添加区域注释,否则会和外边路由冲突
- public class HomeController : Controller
- {
- public IActionResult Index()
- {
- return View();
- }
- }
- }
注意:[Area("Manage")] 需要添加区域注释,否则会和外边路由冲突
- //区域路由 放到上面
- routes.MapAreaRoute("mange_area","Manage","manage/{controller=Home}/{action=Index}/{id?}");