views:

265

answers:

1

Has anyone else had this issue? I have to be doing something wrong.

So I'm setting up a route for errors like this:

routes.MapRoute
(
 "SharedError",
 "Shared/Error/{error}",
 new { error = "" }
);

And calling like:

return parentController.RedirectToRoute("SharedError", new RouteValueDictionary(new { error = errorMessage.ToString() }));

And on the controller:

public ActionResult Error(String error)

Simple right? Well when this is actually run, error is null despite the url looking like:

/Shared/Error/ThisIsTheError

But the error parameter in the Error method is null. (And yes I've tried other words)

Now if I replace all that with the word 'id' everything works.

Global.asax.cs

routes.MapRoute
(
 "SharedError",
 "Shared/Error/{id}",
 new { id = "" }
);

Redirect:

return parentController.RedirectToRoute("SharedError", new RouteValueDictionary(new { id = errorMessage.ToString() }));

Shared Controller:

public ActionResult Error(String id)

Is id a must have word for all routes if you have a default route taking in a value?

+4  A: 

You always need to specify the controller and action parameters:

routes.MapRoute
(
 "SharedError",
 "Shared/Error/{error}",
 new { controller = "Shared", action="Error", error = "" }
);

I'm guessing that the reason why using id works for you is because you have the default route still registered, which has id as a parameter and default values for controller and action.

Note, if you still have the default route, make sure you add your route before it, otherwise it will match the other one first.

DSO
Right, but it should be new { controller="Shared", action="Error", error = "" }
Dennis Palmer
Tried that, still is jacked.
Programmin Tool
Corrected (although I don't think the OP mentioned what his controller name was).
DSO
@programmin: see my note at the end, if that was your problem.
DSO
also make sure you have controller and action set correctly, in the example i just made them up
DSO
Ahhh your note might be it. Checking.
Programmin Tool