1

My main starting page is ApplicantProfile, so my default route looks like this:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "ApplicantProfile", action = "Start", id = UrlParameter.Optional }
);

This controller has no index for public access, but all others do. What I would like is the wildcard equivalent, e.g.

routes.MapRoute(
    name: "Others",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "*", action = "Start", id = UrlParameter.Optional }
);

How can I achieve this?

ProfK
  • 49,207
  • 121
  • 399
  • 775
  • Did any of the answers work for you? If not, could you post how you solved it? – Colin Young Apr 16 '13 at 17:47
  • I actually haven't had chance to experiment. I much higher priority task has intervened, but I will get back to this, soon. – ProfK Apr 17 '13 at 05:49

3 Answers3

4

This should take care of it:

routes.MapRoute(
    name: "Default",
    url: "ApplicantProfile/{action}/{id}",
    defaults: new { controller = "ApplicantProfile", action = "Start", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { action = "Index", id = UrlParameter.Optional }
);

Assuming you have ApplicantProfileController, HomeController and OtherController, this will result in:

  • /ApplicantProfile → ApplicantProfileController.Start
  • /Other → OtherController.Index
  • /SomeOtherPath → default 404 error page
  • / → default 404 error page

See http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs for an introduction to routing. It's a bit old, but it covers the basics well.

Routing happens top-down, meaning it stops at the first match in the routing table. In the first case, you will match your ApplicantProfile route first, so that controller is used. The second case gets Other from the path, finds a matching controller and uses that. The last 2 do not find a matching controller and there is no default specified so a default 404 error is returned. I'd suggest putting in a proper handler for errors. See the answers here and here.

Community
  • 1
  • 1
Colin Young
  • 3,018
  • 1
  • 22
  • 46
  • No, I don't have any such `OtherDefaultController`. I want the request to go to whatever controller is included in the URL. – ProfK Feb 02 '13 at 16:42
  • @ProfK I edited my response to hopefully make things clearer. – Colin Young Feb 02 '13 at 17:52
  • @ColinYoung I don't have and don't want a Home controller, but it looks like I'll have to hammer something in. Maybe just a Controller with an action that invokes the error page. – ProfK Feb 02 '13 at 21:48
  • @ProfK I updated the code example with a tested example that does not specify a default controller and will just fall through to a 404 error. – Colin Young Feb 06 '13 at 14:30
1

This should work as per your requirement

routes.MapRoute(
    name: "ApplicantProfile",
    url: "ApplicantProfile/Start/{id}",
    defaults: new { controller = "ApplicantProfile", action = "Start", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

First one is url which will route you to "Start' Action, other one is default replace "Home" controller with your default one

Satpal
  • 132,252
  • 13
  • 159
  • 168
  • The problem with this solution is that it will route e.g. /ApplicantProfile/Step2 to `HomeController`. The original question isn't explicit about that point, but if I was given the question as a requirement in a real project, that's how I'd interpret it. – Colin Young Feb 04 '13 at 16:16
1

The default should goto profile controller with start action and all the other request should land to index action what ever the controller.

Use the IRouteConstraint to add the constraint to the URL for Other Routes and place that above the default controller with the constraint on the Route for the controller.

You can add a check if the controller is not ApplicationProfile use it.

I hope this helps.

Amit Bagga
  • 648
  • 3
  • 11