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
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.