Using the ASP.NET MVC ActionName attribute

Through convention in ASP.NET MVC the name of the controller and action forms part of the URL. Taking the example below

public class HomeController : Controller
{
    [HttpGet]
    public ActionResult TermsOfUse()
    {
        return View();
    }
}

The URL mydomain.com/home/termsofuse will display the view named TermsOfUse.cshtml from the Views/Home folder. **If using Visual Basic the view name will be TermsOfUse.vbhtml

Adding the ActionName attribute to the Action will change the name of the action to terms-of-use.

[HttpGet]
[ActionName("terms-of-use")]
public ActionResult TermsOfUse()
{
    return View();
}

So mydomain.com/home/termsofuse will not longer work but mydomain.com/home/terms-of-use will work.

Remember to rename your view to match the new action name as the view engine will now be looking for a view called terms-of-use.cshtml, instead of termsofuse.cshtml. The alternative to not renaming the views is to explicitly pass the physical name of the view in to the View method

return View("TermsOfUse");

This would need to be done not only in this action but every time the view needs to be displayed.

Leave a Reply