0

I have a default routing so if I go to www.domain.com/app/ it's, for example, the HomeController. I have another action on the control, e.g. helloworld but if I go to www.domain.com/app/helloworld it fails with a 404 (expecting helloworld controller no doubt).

How can I can non-default actions on my default controller OR how can I map the url /app/helloworld to the helloworld action. My routing looks like this:

routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

routes.MapRoute( //this fails with same 404 like it does when it's ommitted
"Hello", // Route name
"app/helloworld", // URL with parameters
new { controller = "Home", action = "HellowWorld", id = UrlParameter.Optional } // Parameter defaults
);

Basically I need:

/app/ => Controller = Home, Action = Index

/app/helloworld => Controller = Home, Action = HelloWorld, not Controller = HelloWorld, Action - Index

/app/other => Controller = Other, Action = Index

DaveO
  • 1,909
  • 4
  • 33
  • 63

2 Answers2

2

Replace the 2 routes in your Global file with this:

routes.MapRoute(
    "Default", // Route name
    "App/{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

Then verify your HomeController contains:

public ActionResult Index()
{
    return View();
}

public ActionResult HelloWorld()
{
    return View();
}

What this is doing is that yoursite.com/app/ANYCONTROLLER/ANYACTION will route to the controller and action (as long as they exist,) and the default new { controller = "Home", action = "Index" means that if someone goes to yoursite.com/app/ it will automatically route to yoursite.com/app/Home/Index.

If this doesn't work try removing the app/ so it will look like:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

What you had looks like you were trying to route yoursite.com/Home/Index and the second route was phrased wrong, I'm not sure what it would do but instead of "app/helloworld it should have looked like "app/HelloWorld/{action}/{id}" then new { controller = "Home", action = "HellowWorld" would work. So that if someone went to yoursite.com/app it would automatically display yoursite.com/app/Home/HelloWorld. Hope that helps clear some things up.

This is the answer you want

For some reason you don't want to create a new controller for the hello world section, your RegisterRoutes in the Global file should look like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Hello World", // Route name
            "App/HellowWorld/{id}", // URL with parameters
            new { controller = "Home", action = "HelloWorld", id = UrlParameter.Optional }
            );

        routes.MapRoute(
            "Default", // Route name
            "App/{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
    }

Although I would not recommend going about it this way.

Garrett Fogerlie
  • 4,450
  • 3
  • 37
  • 56
  • Sorry these aren't working. I have clarified my routing needs above. Thx – DaveO Aug 18 '12 at 09:41
  • @DaveO see my update, and try `yoursite.com/app/home/helloworld` – Garrett Fogerlie Aug 18 '12 at 10:11
  • @DaveO I assume it worked; I'm curious to know why you didn't want to create a separate `HelloWorld Controller`, or did you end up going that route? – Garrett Fogerlie Aug 18 '12 at 15:10
  • I used your last answer. I didn't want another controller as this particular app only has one and it's a simple response to a form submission. Anyway, it worked brilliantly thanks! – DaveO Aug 19 '12 at 05:35
0

You should just need one route for this. Instead of hard-coding the action, make it a variable, but include the default:

routes.MapRoute( //this fails with same 404 like it does when it's ommitted
    "Hello", // Route name
    "app/{action}", // URL with parameters
    new { controller = "Home", action = "HelloWorld", id = UrlParameter.Optional } 
);

This way, when you go to www.domain.com/app/, it will get the default action (HelloWorld). But you can still specify actions in the route as www.domain.com/app/someotheraction.

Also, keep in mind that the first matching route is selected... so you should consider moving the default route to the end, or leaving it out completely. In the example www.domain.com/app/helloworld, it matches the default route with app as the controller and helloworld as the action (which of course is invalid, hence the 404).

McGarnagle
  • 101,349
  • 31
  • 229
  • 260
  • Thanks. You mean I don't need the two MapRoute calls? I tried just the above and it fails with a white screen. Changing my second call to this still gives me a 404. – DaveO Aug 18 '12 at 04:36
  • @DaveO right-- I'd normally move the first route to the end of the list, that way it functions as a "fallback", without preempting your core routes. – McGarnagle Aug 18 '12 at 04:41