ASP.NET 发送异常消息
ASP.NET send exception message
我正在开发 Web 应用程序,这是我处理异常的方式:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
LogManager.GetCurrentClassLogger().Error(ex);
Response.Clear();
Server.ClearError();
HttpException httpEx = ex as HttpException;
if (httpEx == null)
httpEx = new HttpException(400, ex.Message, ex);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Handler");
routeData.Values.Add("exception", httpEx);
Response.TrySkipIisCustomErrors = true;
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
var c = ControllerBuilder.Current.GetControllerFactory().CreateController(rc, "Error");
c.Execute(rc);
}
发生异常时(例如:throw new ArgumentException("Generator with given Id does not exist.");
),用户会收到包含所发生情况详细信息的错误视图。
问题是,错误消息没有在 HttpResponseMessage 中发送给用户(作为 ReasonPhrase 或其他任何形式)。
private async void DeleteGenerator(Guid id)
{
var response = await dataService.RemoveGenerator(id);
if ((int)response.StatusCode >= 300)
MessageBox.Show( /* Error message from response */ );
}
在这里我应该收到一个盒子,里面有 "Generator with given [...]"
,但我不知道如何实现。我试过 this but the "HttpError" is missing, but I really don't know how to implement it to my code (how to send actual HttpResponseMessage by Application_Error) and this 但我还是不知道应该如何更改它。
编辑
这是我的常规错误处理程序控制器:
public ActionResult Handler(HttpException exception)
{
Response.ContentType = "text/html";
if (exception != null)
{
Response.StatusCode = exception.GetHttpCode();
ViewBag.StatusString = (HttpStatusCode)exception.GetHttpCode();
return View("Handler", exception);
}
return View("Internal");
}
我已经出于测试目的尝试了这个,但它也不起作用(客户端收到 HttpResponseMessage
和 "Bad Request" 作为 ReasonPhrase
。
public HttpResponseMessage Handler(HttpException exception)
{
Response.ContentType = "text/html";
if (exception != null)
{
return new HttpResponseMessage
{
Content = new StringContent("[]", new UTF8Encoding(), "application/json"),
StatusCode = HttpStatusCode.NotFound,
ReasonPhrase = "TEST"
};
}
return null;
}
所以在我的 Controller
中,负责显示错误视图(在 Application_Error
方法中创建和触发)我将 StatusDescription
添加到 Response
.这并不理想,它会覆盖实际的 StatusCode 字符串,但它确实有效。
public class ErrorController : Controller
{
public ActionResult Handler(HttpException exception)
{
Response.ContentType = "text/html";
if (exception != null)
{
Response.StatusCode = exception.GetHttpCode();
Response.StatusDescription = exception.Message;
ViewBag.StatusString = (HttpStatusCode)exception.GetHttpCode();
return View("Handler", exception);
}
return View("Internal");
}
}
我正在开发 Web 应用程序,这是我处理异常的方式:
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
LogManager.GetCurrentClassLogger().Error(ex);
Response.Clear();
Server.ClearError();
HttpException httpEx = ex as HttpException;
if (httpEx == null)
httpEx = new HttpException(400, ex.Message, ex);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Handler");
routeData.Values.Add("exception", httpEx);
Response.TrySkipIisCustomErrors = true;
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
var c = ControllerBuilder.Current.GetControllerFactory().CreateController(rc, "Error");
c.Execute(rc);
}
发生异常时(例如:throw new ArgumentException("Generator with given Id does not exist.");
),用户会收到包含所发生情况详细信息的错误视图。
问题是,错误消息没有在 HttpResponseMessage 中发送给用户(作为 ReasonPhrase 或其他任何形式)。
private async void DeleteGenerator(Guid id)
{
var response = await dataService.RemoveGenerator(id);
if ((int)response.StatusCode >= 300)
MessageBox.Show( /* Error message from response */ );
}
在这里我应该收到一个盒子,里面有 "Generator with given [...]"
,但我不知道如何实现。我试过 this but the "HttpError" is missing,
编辑
这是我的常规错误处理程序控制器:
public ActionResult Handler(HttpException exception)
{
Response.ContentType = "text/html";
if (exception != null)
{
Response.StatusCode = exception.GetHttpCode();
ViewBag.StatusString = (HttpStatusCode)exception.GetHttpCode();
return View("Handler", exception);
}
return View("Internal");
}
我已经出于测试目的尝试了这个,但它也不起作用(客户端收到 HttpResponseMessage
和 "Bad Request" 作为 ReasonPhrase
。
public HttpResponseMessage Handler(HttpException exception)
{
Response.ContentType = "text/html";
if (exception != null)
{
return new HttpResponseMessage
{
Content = new StringContent("[]", new UTF8Encoding(), "application/json"),
StatusCode = HttpStatusCode.NotFound,
ReasonPhrase = "TEST"
};
}
return null;
}
所以在我的 Controller
中,负责显示错误视图(在 Application_Error
方法中创建和触发)我将 StatusDescription
添加到 Response
.这并不理想,它会覆盖实际的 StatusCode 字符串,但它确实有效。
public class ErrorController : Controller
{
public ActionResult Handler(HttpException exception)
{
Response.ContentType = "text/html";
if (exception != null)
{
Response.StatusCode = exception.GetHttpCode();
Response.StatusDescription = exception.Message;
ViewBag.StatusString = (HttpStatusCode)exception.GetHttpCode();
return View("Handler", exception);
}
return View("Internal");
}
}