1

I am trying to store the url in a Session to be picked up later when I redirect the Response to tidy up my URL. This is in the Global.asax file. I am taking the original response and storing the url in a session

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        HttpException httpException = exception as HttpException;

        // get the url
        var url = Request.RawUrl;

        // store in session
        Session["requestUrl"] = url;

        if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404) {
            Response.Redirect("/CMS/CMS");
        }

    }

If I look at the Session contents it is in there

I then try to extract this in my CMS/CMS Action but the variable url is null??:

    public ActionResult CMS(String aspxerrorpath)
    {
        var url = Session["requestUrl"]; // this is null??

        Response.StatusCode = 404;
        Response.TrySkipIisCustomErrors = true;
        return View();
    }

Any Ideas?

CR41G14
  • 5,464
  • 5
  • 43
  • 64
  • possible duplicate of ["HttpContext.Current.Session" vs Global.asax "this.Session"](http://stackoverflow.com/questions/464456/httpcontext-current-session-vs-global-asax-this-session) – Brian Driscoll Jan 04 '13 at 16:04

2 Answers2

2

Darin's approach makes more sense, but in case anyone else has the same problem and needs to set a value in the Session object...

I'm not sure I understand what's going on, but, here's what I found. If you already have a Session variable set, then the code you have works fine. So, for example, if you have something like this:

protected void Session_Start(object sender, EventArgs e) {
 Session["asdasd"] = true;
}

And you hit a page, and then you trigger the error on another request, then the Session[requestUrl] works just fine.

This got to me thinking that perhaps the session cookie is not being sent by ASP.NET if there is an error on the page. Sure enough, if you call, Server.ClearError(); in your Application_Error function, then the Session[requestUrl] works just fine, even if you don't already have a session. Ah ha, I thought, definitely looks like a cookie is not being sent, unless you clear the server error first. However, then I broke out Fiddler, and lo behold a cookie is being sent in both cases. So, I have no idea why calling Server.ClearError(); has the effect of saving your Session variable. :) However, for whatever reason it does work.

aquinas
  • 23,318
  • 5
  • 58
  • 81
1

Sorry I have no idea why the session is not working but if you want to tidy your urls then rewrite the path, don't redirect:

protected void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    var httpException = exception as HttpException;

    if (exception is HttpException && ((HttpException)exception).GetHttpCode() == 404) 
    {
        Server.ClearError();

        // get the url
        var url = Request.RawUrl;

        var routeData = new RouteData();
        routeData.Values["controller"] = "Cms";
        routeData.Values["action"] = "Cms";
        routeData.Values["aspxerrorpath"] = url;

        IController cmsController = new CmsController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        cmsController.Execute(rc);
    }
}

This way you don't need to perform an additional redirect and you don't need to be relying on Session.

You might also checkout the following post from where I borrowed the code.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Of course that it will be passed. You could even send entire complex objects as seen for example in the post I have linked to at the end of my answer. There I am passing the entire `Exception` object to the controller action. – Darin Dimitrov Jan 04 '13 at 16:10
  • This looks good as it preserves the original url also and it does not show CMS/CMS?aspxerrorcode in the URl – CR41G14 Jan 04 '13 at 16:12