Default Route and Route Name in Attribute Routing

Default Route and Route Name in Attribute Routing

In this article, I am going to discuss How to Define Default Route and Route Name using Route Attribute in ASP.NET MVC Application. We are going to work with the same example that we started in the Attribute Routing article of this article series. So, please read Attribute Routing before proceeding to this article.

Default Route in ASP.NET MVC Attribute Routing:

You can also apply the Route attribute on top of the controller (i.e. at the controller level), to capture the default action method as the parameter. That route would then be applied to all actions in the controller; unless a specific Route has been defined on a specific action, overriding the default setting on the controller. Let’s understand this with an example.

namespace AttributeRoutingDemoInMVC.Controllers
{
    [RoutePrefix("MyPage")]
    [Route("action = Index")]
    public class HomeController : Controller
    {
        // URL: /MyPage/
        public ActionResult Index()
        {
            ViewBag.Message = "Index Page";
            return View();
        }

        // URL: /MyPage/Contact
        public ActionResult Contact()
        {
            ViewBag.Message = "Contact page";
            return View();
        }

        // URL: /MyPage/About
        public ActionResult About()
        {
            ViewBag.Message = "About";
            return View();
        }
    }
}
Route Names in Attribute Routing

You can specify a name for a route, in order to easily allow URI generation for it. For example, for the following route:

namespace AttributeRoutingDemoInMVC.Controllers
{
    [Route("menu", Name = "mymenu")]
    public class MenuController : Controller
    {
        public ActionResult MainMenu()
        {
            ViewBag.Message = "Menu Page";
            return View();
        }
    }
}

You could generate a link using Url.RouteUrl:

<a href=”@Url.RouteUrl(“mymenu”)“>Main menu</a>

In the next article, I am going to discuss Entity Framework in ASP.NET MVC Application. Here, in this article, I try to explain the Defining Default Route and Route Name in the ASP.NET MVC application. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.

6 thoughts on “Default Route and Route Name in Attribute Routing”

  1. blank

    Route Names in Attribute Routing
    This is what I did. Place [Route(“menu”, Name = “mymenu”)] in menu controller and place the Main menu in Contact.cshtml of Home controller. Access http://localhost:55831/MyPage/Contact and click on “Main menu” link. It gave an error as “The RouteData must contain an item named ‘action’ with a non-empty string value.”

    This is how I solved it:
    In the Menu controller changed the route to [Route(“menu/{action=Index}”, Name = “mymenu”)]
    Index action of Menu controller is accessible by http://localhost:55831/menu and http://localhost:55831/menu/Index

  2. blank

    When I tried to access mypage/index, mypage/about and mypage/contact, an error took place which says current request is ambiguous between them

Leave a Reply

Your email address will not be published.