Domanda

I'm having rather weird issue. Hoping anyone can shed some light, since I couldn't find an answer anywhere.

Scenario: controller action is called and exception occurs. At the same time I add a new cookie to Response.Cookies.

But no cookie is sent with Response (checked in Fiddler even).

The interesting thing is that the same scenario worked for me before in web forms using generic handler.

Any thoughts?

Code snippet

[HttpGet]
public FileContentResult MyAction()
{
    HttpCookie newCookie = new HttpCookie("error-exc", "error") { HttpOnly = false };
    this.HttpContext.Response.Cookies.Add(newCookie);

    throw Exception("test");
}
È stato utile?

Soluzione

One way to make sure your cookie is sent to the client is to override the OnException() virtual method in your controller:

protected override void OnException(ExceptionContext filterContext) {
    filterContext.ExceptionHandled = true;
    filterContext.HttpContext.Response.StatusCode = 500;
    filterContext.HttpContext.Response.StatusDescription = "Internal server error";
    // whatever...
    base.OnException(filterContext);    
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top