Back to: ASP.NET MVC Tutorial For Beginners and Professionals
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.
Please show the output.
[Route(“action = Index”)] is wrong. It should be [Route(“{action=Index}”)]
This topic is in-complete.
Yes, This is incomplete topic
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
When I tried to access mypage/index, mypage/about and mypage/contact, an error took place which says current request is ambiguous between them
Define Default action method or
Route for each action method…
Then only it will work…
Defining only the routeprefix will identify only the controller… For that there have no action method defined in the route table… In the field of action column the value is null… You need to set it to a default value or a specific value … Then only the url will map to the controller and action method….
Hope you understand…
Define a default action method or a specific action method…
Defining only the routeprefix will only define the controller in the route table… The action field value is empty…and hence it will say ambiguous …
You must have to put a default or specific value in that field to map your url to a specific controller and action method…
Because the route based url is managed by the route table where we map url to a controller and action method ….
For route attributes defied at the top of the action methods where we don’t define controller or action .
Like in a programme if,
Controller Name: HomeController
Action Name: Contact
Defied route attribute at the top of the Contact Action: Route(“abc/{de}/efg”)
The in the route table it will be stored by internally like:
Url: “abc/{de}/efg”
Controller: Home
Action: Contact