Martin at Mennt
Martin at Mennt

Reputation: 5737

URL different from view name ASP MVC 3

Is it possible to add some sort of meta data to a view to make the URL to be different than the view name?

Example:

[DisplayName("My-View")]
public ActionResult MyView()
{
   return View();
}

As you see, I would like the url to be www.mydomain.com/My-View not www.mydomain.com/MyView.

I guess that can be sorted with some routing or IIS rewriting, but there really should be som sort of meta functionality to this.

Upvotes: 2

Views: 925

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039130

[ActionName("My-View")]
public ActionResult MyView()
{
    return View();
}

Upvotes: 2

pjvds
pjvds

Reputation: 956

Use the ActionName instead of DisplayName: ActionNameAttribute

This attribute will allow you to to start your action with a number or include any character that .net does not allow in an identifier. Via this way you can have nice URL's without the hassle of rewriting via other methods.

Example

[ActionName("My-View")]
public ActionResult MyView()
{
   return View();
}

Upvotes: 4

Related Questions