Search code examples
asp.netasp.net-mvc-4custom-url

asp.net mvc 4 custom urls for action method


I am creating a asp.net mvc 4 application

public class AspNetController : Controller
{
    //
    // GET: /AspNet/
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Introduction()
    {
        return View();
    }
}

as Shown Above There is AspNet Controller and Introduction Action Method

Default Url for Introduction Action Method is

localhost:55134/aspnet/introduction

But I Want Url Like

localhost:55134/aspnet/introduction-To-AspNet

Same for /localhost:55134/aspnet/NetFrameWork To

/localhost:55134/aspnet/What-is-.Net-Framework

How to do that


Solution

  • You should be able to use the ActionName attribute to decorate your routes.

    [ActionName("Introduction-To-AspNet")]
    public ActionResult Introduction()
    {
        return View();
    }
    

    You really want to use AttributeRouting, either via a 3rd party package or natively if you can.