Christoph Fink
Christoph Fink

Reputation: 23093

Get Response Content in MVC

I am implementing an error handler for my MVC 4 application following THIS.
Now I am doing about the same for all errors not only 404 and I would like to get the original response to save it for logging or display it if in debug mode as it includes usefull information, like complete stack-traces, small display where in the code the error happened and so on.

I am now struggeling with getting the buffered repsonse data out of the Response object before calling Response.Clear(), but can't figure out how.

How can I get the content of a HttpResonse object?

For the HttpWebResponse there is a GetResponseStream() method which can be used for that, where I found a lot of examples, but nothing for the HttpResonse and OutputStream can't be read...

Upvotes: 2

Views: 2797

Answers (1)

davethecoder
davethecoder

Reputation: 3932

not sure if this may help into the right direction, but i just use this:

[AttributeUsage(AttributeTargets.Class)]
public class ErrorHandlerAttribute : FilterAttribute, IExceptionFilter
{

    readonly BaseController _bs = new BaseController();

    public virtual void OnException(ExceptionContext fc)
    {
        var model = new errors
        {
            stacktrace = fc.Exception.StackTrace,
            url = fc.HttpContext.Request.RawUrl,
            controller = fc.RouteData.GetRequiredString("controller"),
            source = fc.Exception.Source,
            errordate = DateTime.Now,
            message = fc.Exception.Message//,
            //InnExc = String.IsNullOrEmpty(fc.Exception.InnerException.ToString()) ? fc.Exception.InnerException.ToString() : ""
        };

        var message = "<html><head></head><body><h2>An error occured on " + _bs.GetKeyValue<string>("Mobile Chat App") + ".</h2>";
        message += "<strong>Message:</strong> <pre style=\"background-color:#FFFFEF\"> " + model.message + "</pre><br />";
        message += "<strong>Source:</strong> <pre style=\"background-color:#FFFFEF\">" + model.source + "</pre><br />";
        message += "<strong>Stacktrace:</strong><pre style=\"background-color:#FFFFEF\"> " + model.stacktrace + "</pre><br />";
        message += "<strong>Raw URL:</strong> <pre style=\"background-color:#FFFFEF\">" + model.url + "</pre></br />";
        message += "<strong>Inner Exception:</strong> <pre style=\"background-color:#FFFFEF\">" + model.InnExc + "</pre></br />";
        message += "<strong>Any Form values</strong>: <pre>" + fc.HttpContext.Request.Form + "</pre><br />";
        message += "</body></html>";

        fc.ExceptionHandled = true;
        fc.HttpContext.Response.Clear();
        fc.HttpContext.Response.StatusCode = 500;
        fc.HttpContext.Response.TrySkipIisCustomErrors = true;

        _bs.SendErrorMail(message);

        fc.ExceptionHandled = true;
        //var res = new ViewResult { ViewName = "error" };
        //fc.Result = res;
        fc.HttpContext.Response.Redirect("/ErrorPage");
        //fc.HttpContext.Response.RedirectToRoute("error",new{controller="Home",action="ErrorPage"});
    }

I have a gmail errors account that i throw all errors into, and lets me know if anything goes wrong with any site i make, this has not failed me yet with information. I have not looked into this with great extent, as i use a different method these days but:

fc.HttpContext.Request.RawUrl, 

is just the httpContext, do you have hold of the httpcontext, I use the URL for get and fc.HttpContext.Request.Form to get any and all form data that may have caused the error.

This is not the response like you asked, but this is because i catch the errors during request, rather than an error with response!! I then clear the response ( would be an error 500 ) and replace this with a redirect to my page, that has a nice picture of a monkey with a keyboard :-)

Upvotes: 1

Related Questions