3

I have MVC3 application, where I want to keep short URL. what is the best or clean way to do this? Lets say I have two controllers Account and Home. I have all the account related tasks Logon, Logoff, Profile, FAQs etc. in Account controller. All the main tasks in home controller like TaskA, TaskB, and TaskC. I am looking for URL as below:

  1. www.mydomain.com/Logon
  2. www.mydomain.com/Logoff
  3. www.mydomain.com/Profile
  4. www.mydomain.com/FAQs
  5. www.mydomain.com/TaskA
  6. www.mydomain.com/TaskB

when user first come to the website they need to redirect to Logon page. At any time user should also able to switch from once controller action to another controller action (from TaskA to Logoff).

what is the clean way to do this?

matmat
  • 2,366
  • 5
  • 19
  • 23

2 Answers2

6

You can set a route for the specific urls that do not match the default route. For example:

routes.MapRoute("Logon", "logon/", new { controller = "account", action = "logon" });
routes.MapRoute("TaskA", "TaskA/", new { controller = "home", action = "taska" });

Your default route can define your start page if all other matches for the url are not found.

routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}/", // URL with parameters
            new { controller = "account", action = "logon", id = UrlParameter.Optional } // Parameter defaults
CRice
  • 12,279
  • 7
  • 57
  • 84
  • 1
    Thanks for your response. based on your idea, I can handle this situation by creating one route for each action method. so if I have 50 actions, I end up creating 50 routes. I am looking for something which require minimum amount of code. – matmat Jun 20 '11 at 13:27
  • didn't know you had so many, what about what frennky suggests then – CRice Jun 20 '11 at 23:14
  • It did not work. Lets say user successfully Logged on, In the Post action method of "LogOn" I am redirecting as RedirectToAction("TaskA"); TaskA belongs to "Home" controller. However MVC runtime looking for url "/Account/TaskA". when we do RedirectToAction MVC automatically added current controller in the URL. If I do like RedirectToAction("TaskA", "Home"); it will work, but then I wont get short url,any idea how to resolve this situation? – matmat Jun 21 '11 at 20:03
6

You don't need to set a route for each URL. With a little help from route constraints you can do something like this:

        routes.MapRoute(
            "Home", // Route name
            "{action}", // URL with parameters
            new { controller = "Home", action = "Index" }, // Parameter defaults
            new { action = "TaskA|TaskB|TaskC|etc" } //Route constraints
        );

        routes.MapRoute(
            "Account", // Route name
            "{action}", // URL with parameters
            new { controller = "Account", action = "Logon" }, // Parameter defaults
            new { action = "Logon|Logoff|Profile|FAQs|etc" } //Route constraints
        );
frennky
  • 12,581
  • 10
  • 47
  • 63
  • that looks like a good solution - hopefully there are no duplicate action names. – CRice Jun 20 '11 at 14:01
  • It did not work. Lets say user successfully Logged on, In the Post action method of "LogOn" I am redirecting as RedirectToAction("TaskA"); TaskA belongs to "Home" controller. However MVC runtime looking for url "/Account/TaskA". when we do RedirectToAction MVC automatically added current controller in the URL. If I do like RedirectToAction("TaskA", "Home"); it will work, but then I wont get short url,any idea how to resolve this situation? – matmat Jun 21 '11 at 20:00
  • 1
    @matmat If you don't specify a controller in a RedirectToAction it will assume you mean current. That is the way MVC framework works. How can it know which controller you actually meant? If you specify the controller RedirectToAction("TaskA", "Home") it will construct a short url (without controller name). The solution I provided works, I wouldn't have provided an answer before I test it. – frennky Jun 22 '11 at 08:16