Dynamic MVC Routing in asp.net mvc

Total Post:48

Points:336
Mvc 
 1572  View(s)
Ratings:
Rate this:

I have Controller name: About and Action name: Index. But I want the URL to be like this (action name will be dynamically)

www.example.com/home/aaa www.example.com/home/bbb www.example.com/home/ccc

Routing

  routes.MapRoute(
                name: "Home",
                url: "{controller}/{name}",
                defaults: new { controller = "Home", action = "Index"}

Controller

public class AboutController : Controller
    {
        public ActionResult Index(string name)
        {
            return View();
        }
    }
}

View

@{
    ViewBag.Title = "Index";
}
<h2>Index About</h2>

  1. Post:13

    Points:91
    Re: Dynamic MVC Routing in asp.net mvc

    This should work.

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

    Make sure your default route exists and comes after About route

      Modified On Apr-06-2018 05:22:18 AM

Answer