Create a friendly URL in ASP.NET MVC ( Multiple Routes )

You can generate a friendly URL in ASP.NET MVC.


There is a way to create a friendly URL.


You can create a new route ( custom route ) using the MapRoute extension method or change the default route in the RouteConfig class.


For default route;


routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}/{ignore}",
    defaults: new { controller = "Home", action = "Index",
        id = UrlParameter.Optional, ignore = UrlParameter.Optional }
);

You can type whatever you want to {ignore}. The application will ignore it.


You can register multiple custom routes with different names.
Consider the following example where you register "Question" route.


For a new route;


routes.MapRoute(
   name: "Question",
   url: "question/{id}/{ignore}",
   defaults: new
   {
       controller = "Question",
       action = "Index",
       id = UrlParameter.Optional,
       ignore = UrlParameter.Optional
   }
);

If you don't want to change the default route, you can generate a new route that looks like this.


If you have noticed, there is no the action path. You may not want the action path.


The Question route is created for the Question controller and the Index action. So you learned how to hide the action name from route.


using System.Web.Mvc;
using System.Web.Routing;

namespace WebApplication1
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
               name: "Question",
               url: "question/{id}/{ignore}",
               defaults: new
               {
                   controller = "Question",
                   action = "Index",
                   id = UrlParameter.Optional,
                   ignore = UrlParameter.Optional
               }
           );

            routes.MapRoute(
                 name: "Default",
                 url: "{controller}/{action}/{id}",
                 defaults: new
                 {
                     controller = "Home",
                     action = "Index",
                     id = UrlParameter.Optional
                 }
             );


        }
    }
}

b1.PNG

using System.Web.Mvc;

namespace WebApplication5.Controllers
{
    public class QuestionController : Controller
    {
        public ActionResult Index(int? id)
        {
            return View();
        }
    }
}

You can use the constraint pattern to make sure that id is a numeric value.


The pattern is id = @"\d+"


- Modify the new route and the default route as shown below.


routes.MapRoute(
   name: "Question",
   url: "question/{id}/{ignore}",
   defaults: new
   {
       controller = "Question",
       action = "Index",
       id = UrlParameter.Optional,
       ignore = UrlParameter.Optional
   },
   constraints: new { id = @"\d+" }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new
    {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    },
    constraints: new { id = @"\d+" }
);

\d is a digit (a character in the range 0 - 9 ) and + means 1 or more digits.


b2.PNG

tags : asp.net mvc 5 url friendly, Multiple Routes in ASP.NET MVC