0

I would like to make some urls of my asp.net MVC4 app shorter. For example I have Account controller and such action ForgotPassword. Url looks like this

http://www.mydomain.com/account/forgotpassword

I would like to make the url shorter(example below) without renaming actual controller and action names. What is the best way to do that?

http://www.mydomain.com/a/fp
Tomas
  • 17,551
  • 43
  • 152
  • 257

3 Answers3

2

You could register a simple route:

routes.MapRoute(
   name: "ForgottenPassword",
   url: "a/fp",
   defaults: new { controller = "Account", action = "ForgottenPassword" }
);

...in RouteConfig.cs if you're using MVC4.

Lee Gunn
  • 8,417
  • 4
  • 38
  • 33
0

If i am not wrong, then your talking about Friendly URL's?

Please have a look @ quysnhat.wordpress.com

There was a very nice post in Hanselman's web.

Also, there were few questions related to friendly url's(in case it helps you) :-

How can I create a friendly URL in ASP.NET MVC?

http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx

Community
  • 1
  • 1
Shubh
  • 6,693
  • 9
  • 48
  • 83
0

The easiest but not the best way of doing it is to hand-code your custom routes:

routes.MapRoute(
    name: "AccountForgotRoute",
    url: "a/fp/{id}",
    defaults: new { controller = "Account", action = "ForgotPassword", id = UrlParameter.Optional }        
);

The downside to that is if you will have tons of controllers and action methods and you like all of them to be "shortened", then you will have to write a lot of routes.

One alternative is to define your custom routes in a database and write it out on a file. So for example you have in a database row an accountcontroller-forgotpassword key with a value of a/fp, you dynamically build the route definition, write it in a file and let your application pick it up. How your application can pick up the route definition can be done like this. That link is still applicable for MVC 4. But this one is really messy, IMO, but is an alternative.

von v.
  • 16,868
  • 4
  • 60
  • 84