Search code examples
c#asp.netasp.net-mvcrouteconfig

How can I do route configuration in ASP.NET MVC?


How can I do route configuration as below?

My current url is: http://localhost:4815/Home/ByCategory/1

But I want it to be: http://localhost:4815/CategoryTitle

public ActionResult ByCategory(int? id)
{
    ...
}

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

Solution

  • Thank you for your suggestions. I have added the following code to routeconfig. I used <a href="/question"> </a> on the view page to go to the relevant controller

    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
    
                routes.MapRoute(
                   name: "AddQuestion",
                   url: "AddQuestion",
                   defaults: new { controller = "Question", action = "Create" }
               );