0

In my MVC application , RouteConfig contains following code (by default):

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{key}",
            defaults: new { controller = "Home", action = "Index", key= UrlParameter.Optional }
        );
    }

When a user hits my website (abc.com) or type abc/Home/Index , it invokes Home/Index. Problem is that I want to use key. I mean If a user enters abc/someKey or abc/Home/Index/someKey it should go to Home/Index where i can access that key. It works good in abc/Home/Index/someKey case but fails in abc/someKey & gives exception. Is there any other way to get that functionality ?

Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
  • Because if you use `abc/someKey` it is trying to find a method name `Index` on `someKeyController`. You need to create specific routes –  Sep 02 '15 at 08:07
  • I know this. But I want to change the route file for the required stuff. I do have thousands of keys & certainly can not implement that many controllers. @StephenMuecke – Baqer Naqvi Sep 02 '15 at 08:11
  • The number of keys you have has nothing to do with it (you just pass the value as a parameter). But you will never be able to to use just `/abc/someKey` You need at least the controller name in the route –  Sep 02 '15 at 08:15
  • okay, how can I create specific routes in this case ? @StephenMuecke – Baqer Naqvi Sep 02 '15 at 09:00
  • It depends what you want to do but a route needs to be distinguishable. `abc/someKey` cannot work because its looking for `someKeyController`. You could do `abc/Home/someKey` to have you always go to the `Index` method of `HomeController`. But what is the issue with `abc/Home/Index/someKey` anyway? –  Sep 02 '15 at 09:21
  • Actually I want to implement subdomain concept. Every user can create its own domain on abc.com. I have another idea, I create a KeyController, if controller is not Home, then move to that KeyController > Index but don't know how to do it. :/ @StephenMuecke – Baqer Naqvi Sep 02 '15 at 09:31
  • You can make any URL scheme you want (including responding to subdomains) by using a [custom RouteBase subclass](http://stackoverflow.com/questions/31934144/multiple-levels-in-mvc-custom-routing/31958586#31958586). However, without more specifics of what you are trying to achieve, I can't give you a better example than that one. – NightOwl888 Sep 04 '15 at 10:14

0 Answers0