asp.net-mvcasp.net-mvc-4asp.net-mvc-routingmvcroutehandler

Dynamic MVC Routing : Action name


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

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

Routing

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

Controller

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

View

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>

Solution

  • This should work.

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

    Make sure your default route exists and comes after About route