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

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.

主站蜘蛛池模板: 普格县| 和平县| 永清县| 竹溪县| 吉首市| 旬邑县| 荣成市| 菏泽市| 新干县| 安岳县| 阿巴嘎旗| 宝丰县| 宝兴县| 宁远县| 儋州市| 承德县| 鄂伦春自治旗| 济阳县| 营口市| 高碑店市| 阳信县| 顺平县| 孝昌县| 广州市| 鹰潭市| 县级市| 新民市| 张家川| 罗定市| 武冈市| 牟定县| 台湾省| 多伦县| 津南区| 龙陵县| 灌云县| 常山县| 策勒县| 邯郸市| 武山县| 喀什市|