官术网_书友最值得收藏!

Adding routes via route attributes

Adding routes via route attributes is a way of defining the route for every controller action as an attribute on top of controller action methods.

Within the controller class (in our case, HomeController), add the Route attribute before the Index method:

[Route("Home/Index")]
public IActionResult Index()
{
return View();
}

This instructs ASP.NET Core MVC to pass requests to the Index method whenever the URL looks like http://example.com/Home/Index. As patterns are not supported in attribute routing, if we want to make the Index method of HomeController the default of the application, then we need to add multiple Route attribute calls on top of the Index method:

[Route("")]
[Route("Home")]
[Route("Home/Index")]
public IActionResult Index()
{
return View();
}

Attribute routing makes it easier to add new routes and keep them up-to-date with name changes or architectural code changes, but it doesn't support complicated patterns and makes it harder to see the full route list of the application. Attribute routing is, in most cases, the preferred way for ASP.NET Core web APIs.

主站蜘蛛池模板: 常宁市| 谷城县| 滨海县| 衡山县| 原平市| 秭归县| 德格县| 白朗县| 海盐县| 天峨县| 揭东县| 罗甸县| 常山县| 大英县| 怀化市| 二连浩特市| 襄垣县| 个旧市| 喀喇沁旗| 阿图什市| 安仁县| 吕梁市| 土默特右旗| 嘉鱼县| 视频| 浪卡子县| 乐至县| 遂宁市| 永登县| 留坝县| 安康市| 长宁县| 康平县| 岳阳县| 高青县| 合阳县| 炎陵县| 陇南市| 崇左市| 阳信县| 福海县|