.net Core - Default controller is not loading when Route attribute is used

回眸只為那壹抹淺笑 提交于 2019-12-05 04:52:40

From the asp docs:

Actions are either conventionally routed or attribute routed. Placing a route on the controller or the action makes it attribute routed. Actions that define attribute routes cannot be reached through the conventional routes and vice-versa. Any route attribute on the controller makes all actions in the controller attribute routed.

So basically if you use attributes then the routes defined in UseMvc or UseMvcWithDefaultRoute will be ignored. Only the attribute would be used in that case.

You could still use multiple route attributes if you wanted to achieve a similar effect than the default route with optional segments. Again from the same article in the asp docs:

public class MyDemoController : Controller
{
   [Route("")]
   [Route("Home")]
   [Route("Home/Index")]
   public IActionResult MyIndex()
   {
      return View("Index");
   }
   [Route("Home/About")]
   public IActionResult MyAbout()
   {
      return View("About");
   }
   [Route("Home/Contact")]
   public IActionResult MyContact()
   {
      return View("Contact");
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!