【问题标题】:Client doesn't receive custom HttpResponseException from ApiController客户端没有收到来自 ApiController 的自定义 HttpResponseException
【发布时间】:2015-07-16 08:05:43
【问题描述】:

我为我的 Web API 控制器操作创建了一个异常过滤器,但它似乎没有做任何事情(即使它确实被调用了)。

属性

public class ExceptionHandlerAttribute : ExceptionFilterAttribute
{
    public override void OnException(HttpActionExecutedContext context)
    {
        context.Response = new HttpResponseMessage(HttpStatusCode.BadRequest);
        context.Response.Content = new StringContent("My content");
        context.Response.ReasonPhrase = "My reason";
    }
}

我也试过了:

public override void OnException(HttpActionExecutedContext context)
{
    throw new HttpResponseException(
        new HttpResponseMessage(HttpStatusCode.BadRequest)
        {
            Content = new StringContent("The content"),
            ReasonPhrase = "The reason"
        });
}

控制器

[ExceptionHandler]
public class MyController : ApiController
{
    [Route("MyRoute"), HttpGet]
    public MyModel Index() {
        // code causing exception
    }
}

WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Filters.Add(new ExceptionHandlerAttribute());
    }
}

但是,当发生异常时,客户端会收到以下信息:

【问题讨论】:

  • 它被调用了吗?
  • 是的,它正在被调用。

标签: c# .net asp.net-mvc asp.net-web-api


【解决方案1】:

您需要在异常过滤器的响应中抛出 HttpResponseException

public override void OnException(HttpActionExecutedContext context)
{
    throw new HttpResponseException(
        new HttpResponseMessage(HttpStatusCode.BadRequest)
        {
            Content = new StringContent("The content"),
            ReasonPhrase = "The reason"
        });
}

这里有更多关于how to handle exceptions in Web API的详细信息。

【讨论】:

    【解决方案2】:

    您的 API 端代码没问题。但得到响应应该是这样的(使用 WebException 捕获):

    string getResponse(WebRequest request, out exceptionOccured)
    {
        exceptionOccured = false;
        try
        {
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            var stream = resp.GetResponseStream();
            using (var reader = new StreamReader(stream))
            {
                return reader.ReadToEnd();
            }
        }
        catch (WebException ex)
        {
            exceptionOccured = true;
            using (var stream = ex.Response.GetResponseStream())
            {
                using (var reader = new StreamReader(stream))
                {
                    return reader.ReadToEnd();
                }
            }
        }
        catch (Exception ex)
        {
            exceptionOccured = true;
            // Something more serious happened
            // like for example you don't have network access
            // we cannot talk about a server exception here as
            // the server probably was never reached
            return ex.Message;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-23
      • 2018-11-02
      • 2020-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多