Imran K
Imran K

Reputation: 25

Change url (redirection) in asp .NET

I am working on a C# mvc application. In my site I have this url abc.com/About/WhoWeAre, where 'About' is the Controller and 'WhoWeAre' is the action name. But i want this url to be returned as abc.com/About/who-we-are. The problem is I can't name the action containing '-' in it. I tried Url Redirection using HttpContext Response but couldn't find a solution.

If I handle the request in Route Config for 'About/who-we-are' and route it to 'About/WhoWeAre' it is working with the required url in the address bar. But when I make request for 'About/WhoWeAre' it returns the page with the same('About/WhoWeAre') url in the address bar, which duplicates the url. How can I redirect?

Feel free to ask any questions.

Upvotes: 0

Views: 1092

Answers (2)

Ashish Shukla
Ashish Shukla

Reputation: 1294

Use ActionName Attribute to map the Url. Below is the example

 public class AboutController : Controller
 {

    [ActionName("Who-we-are")]
    public ActionResult WhoWeAre()
    {
        return View();
    }
 }

Upvotes: 2

ScoobyDrew18
ScoobyDrew18

Reputation: 631

You can use the ActionName attribute to rename your actions with characters that are disallowed in C# method names.

Upvotes: 0

Related Questions