Published on: 01 March, 2018

How to enable attribute routing in C# MVC?

Posted by andy | 96 views 0 likes 0 favourites 0 comments
Tumblr Digg Google Plus

If you want to enable routing in C# MVC, you have to do the following steps. Note: this only applies to MVC version 5 or above. Open your RouteConfig.cs file under App_Start folder in your MVC root project.

Add the following code after the IngoreRoute section.


The above code will enable the MVC Attribute Routes mapping. If you do not apply above code, it will just ignore the attribute routing.

The next part is to set the attribute routing in your MVC controller.

For example, I have a page named ForgotPassword and I want to add a hyphen for SEO friendly URL page, so the URL that can be accepted will be forgot-password. In order to fix it, I can use the route prefix and placed it above my ForgotPasswordController class as below.


You can see the screenshot of example of the RoutePrefix including the sample of how to overwrite the routeprefix if needed.


There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.
Share your article with us and add your own google adsense account to earn extra money. Plus, you can promote a link back to your site.
Related Articles
Published on: 21 March, 2018
In this tutorial you will learn how to create your own custom identity authentication and authorization with ASP.Net MVC without using Entity Framework. By default, the example given in the MVC official tutorial site is using Entity Framework. So if you do not want to use Entity Framework and want to use external data source or your own database, you may want to read the following article I wrote on how to integrate your existing login details.
Published on: 27 October, 2017
In this article, you will learn how to download a file in Web API MVC. We will use HTTP Get API method to perform this download action with one single parameter which will accept the file name.
Published on: 28 October, 2016

In this tutorial you will learn how easily you can implement a simple checkout express using C# MVC Web API. We will create a really simple shopping cart where customers can add and delete their cart items before proceed to payment.