Ole Lynge
Ole Lynge

Reputation: 4587

Missing RouteData DataTokens

I'm handling errors in ASP.NET MVC by sending an error id from Application.OnError to a controller action, by adding a route data value that gives the error id:

Global.asax.cs/OnError:

var routeData = new RouteData();
routeData.DataTokens.Add("errorKey", errorId);
var context = new RequestContext(new HttpContextWrapper(Context), routeData);
errorController.Execute(context);

And then reading it in the controller/action:

object errorKey = RouteData.DataTokens["errorKey"];

On my local machine it runs fine, but on the servers I have tried the errorKey is not passed along.

What could be possible reasons for this?


A new observation, when the site is running a web server:

Upvotes: 0

Views: 2458

Answers (2)

Ole Lynge
Ole Lynge

Reputation: 4587

I found the reason: In web.config was set the following:

<system.webServer>
<httpErrors errorMode="Custom">

which caused the action to be executed twice: the first time with the errorKey, but the second and deciding time without the errorKey. I changed it to

<system.webServer>
<httpErrors errorMode="Detailed">

and the action was only executed once, with the errorKey.

Upvotes: 1

Alexandre Brisebois
Alexandre Brisebois

Reputation: 6743

i've never tried to use the route data in such a way, you might want to look into this instead

HttpContext.Items["errorKey"]

the items are scoped to your request.

you may also want to look at TempData["errorKey"] found on the controller :

ASP.NET TempData persists between requests

as for the values in the route data, have a look at RouteData.Values["errorKey"]

Upvotes: 2

Related Questions