0

I don't know why I have such problems with ASP.NET MVC routing. I wish there was a tool that showed me which routes I had currently setup. Regardless,

In my global.asax.cs file I have the following:

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

  routes.MapRoute(
    "SignUp", // Route name
    "account/{controller}/{action}", // URL with parameters
    new { controller = "Home", action = "Register" } // Parameter defaults
  );

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

I have the following defined in HomeController.cs

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

I was expecting to be able to access this page by visiting /account/register in my browser. However, I continue to get a 404. What am I doing wrong?

1

3 Answers 3

3

/Account/Register matches your first route.

The word Register is matched to the {controller}, so it looks for a controller named RegisterController.

Sign up to request clarification or add additional context in comments.

Comments

1

replace

routes.MapRoute(
    "SignUp", // Route name
    "account/{controller}/{action}", // URL with parameters
    new { controller = "Home", action = "Register" } // Parameter defaults
  );

with

routes.MapRoute(
    "SignUp", // Route name
    "account/{action}", // URL with parameters
    new { controller = "Home" } // Parameter defaults
  );

This will mean /account/register will route to the Register action on the Home controller. It will also mean that action links and other links you generate via @Html.ActionLink("Register", "Register", "Home") will generate the URL /account/register

Think of the 'URL with paramters' as a pattern that the URL will be matched against.

The problem with your original route map is that it is looking for a URL like this /account/controllername/actionname. So, when you go /account/register - it is taking register as the controller name, and taking the default action name (in this case register) - and as the 'register' action does not exist in the 'register' controller - you are getting a 404.

UPDATED I updated my suggested route as per Robert's comments.

It is also worth noting, as Robert states, that this whole thing could be made more simple by making a 'Account' controller, and moving the 'Register ' action there. Then you could delete the 'SignUp' route, and just use default routing. If you thought about it, you'd agree that this would be a better place for a 'Register' action than the 'Home' controller.

2 Comments

to actually use account/register they should remove default action value because otherwise registration would be accessible from account only. Generated URLs will never have the word register in them. If you write URL manually it will of course work just as well.
BTW: I would also recommend (and is likely not the case) to avoid having account actions in HomeController. It would be better to have it elsewhere. AccountController maybe? Well in that case default routing would still apply, because controller's name would be account... Which means that OP is most likely overcomplicating their routing for no obvious benefit. They should rather just stay with default routing and separate controller...
0

Try using this nugget package http://nuget.org/packages/Glimpse.Mvc3 You can find more info about glimpse on http://getglimpse.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.