Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MVC中获取响应内容_C#_.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 在MVC中获取响应内容

C# 在MVC中获取响应内容,c#,.net,asp.net-mvc,asp.net-mvc-4,C#,.net,Asp.net Mvc,Asp.net Mvc 4,我正在为我的MVC 4应用程序实现以下错误处理程序。 现在,我对所有错误都做了同样的处理,不仅仅是404,我希望得到原始响应,以便在调试模式下保存或显示它,因为它包含有用的信息,如完整的堆栈跟踪、错误发生在代码中的小显示等等 在调用Response.Clear()之前,我正在努力从Response对象中获取缓冲的repsonse数据,但不知道该如何处理 如何获取HttpResonse对象的内容? 对于HttpWebResponse有一个GetResponseStream()方法可以用于此,在这里

我正在为我的MVC 4应用程序实现以下错误处理程序。
现在,我对所有错误都做了同样的处理,不仅仅是404,我希望得到原始响应,以便在调试模式下保存或显示它,因为它包含有用的信息,如完整的堆栈跟踪、错误发生在代码中的小显示等等

在调用
Response.Clear()
之前,我正在努力从
Response
对象中获取缓冲的repsonse数据,但不知道该如何处理

如何获取HttpResonse对象的内容?


对于
HttpWebResponse
有一个
GetResponseStream()
方法可以用于此,在这里我找到了很多示例,但是对于
httpresponse
OutputStream
没有任何内容是无法读取的…

不确定这是否有助于正确的方向,但我只是使用了以下内容:

[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"});
    }
只是httpContext,您是否拥有httpContext,我使用get的URL和fc.httpContext.Request.Form来获取可能导致错误的任何和所有表单数据


这不是您要求的响应,但这是因为我在请求期间捕捉到错误,而不是响应中的错误!!然后,我清除响应(可能是错误500),并将其替换为重定向到我的页面,该页面有一个带键盘的猴子的漂亮图片:-)

异常不包括默认错误为您准备的所有内容,但现在我走这条路。如果没有人很快给出更好的答案,我会接受你的答案…是的,我知道,但是我用上面的方法基本上给我YSOD,给我的电子邮件,这可能足以调试你的代码/解决方案,而不需要太多其他的东西。真的,我也使用try/catch,并将异常发送给电子邮件编译器,这样我就不会错过任何技巧,我学到的一个好技巧是包含浏览器类型,这帮助我发现了一个只针对黑莓浏览器的bug
fc.HttpContext.Request.RawUrl,