Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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# Web Api异常处理_C#_Asp.net Mvc_Exception Handling - Fatal编程技术网

C# Web Api异常处理

C# Web Api异常处理,c#,asp.net-mvc,exception-handling,C#,Asp.net Mvc,Exception Handling,在我的应用程序中,我尝试使用异常处理。我试图做的是创建一个单独的类,并从HttpResponseException扩展它 这是我一直写到现在的代码 public class ApiExceptions : HttpResponseException { public ApiExceptions(string reason, HttpStatusCode code) { var response = new HttpResponseMessage {

在我的应用程序中,我尝试使用异常处理。我试图做的是创建一个单独的类,并从HttpResponseException扩展它

这是我一直写到现在的代码

public class ApiExceptions : HttpResponseException
{
    public ApiExceptions(string reason, HttpStatusCode code)
    {
        var response = new HttpResponseMessage
        {
            StatusCode = code,
            ReasonPhrase = reason,
            Content = new StringContent(reason)
        };
        throw new HttpResponseException(response);
    }
}
但是我得到了这个错误

'System.Web.Http.HttpResponseException' does not contain a constructor that takes 0 arguments

我对c#中的异常处理非常陌生。我真的很感谢你在这方面的帮助

HttpResponseException类,这两个类都需要一个值:

HttpResponseException(HttpResponseMessage)
HttpResponseException(HttpStatusCode)

因为您的
apiexception
类继承自
HttpResponseException
,所以它必须提供兼容的构造函数。由于该类只返回
HttpResponseException
,因此它似乎根本不需要从该类继承。

HttpResponseException类,这两个类都需要一个值:

HttpResponseException(HttpResponseMessage)
HttpResponseException(HttpStatusCode)

因为您的
apiexception
类继承自
HttpResponseException
,所以它必须提供兼容的构造函数。由于该类所做的只是返回HttpResponseException,因此似乎根本不需要从该类继承

:base(…)

如果省略对基构造函数的调用,它将自动调用默认的基构造函数,并且HttpResponseException没有无参数

 public class ApiExceptions : HttpResponseException
        {
            public ApiExceptions(string reason, HttpStatusCode code):base(code)
            {
                var response = new HttpResponseMessage
                {
                    StatusCode = code,
                    ReasonPhrase = reason,
                    Content = new StringContent(reason)
                };
                throw new HttpResponseException(response);
            }
        }

无论如何,我不确定为什么需要扩展HttpResponseException,下面的代码应该可以工作

:base(…)

如果省略对基构造函数的调用,它将自动调用默认的基构造函数,并且HttpResponseException没有无参数

 public class ApiExceptions : HttpResponseException
        {
            public ApiExceptions(string reason, HttpStatusCode code):base(code)
            {
                var response = new HttpResponseMessage
                {
                    StatusCode = code,
                    ReasonPhrase = reason,
                    Content = new StringContent(reason)
                };
                throw new HttpResponseException(response);
            }
        }

无论如何,我不确定为什么需要扩展HttpResponseException

如果您想从HttpResponseException派生,那么应该使用类似的代码:在示例中是一个自定义异常,返回BadRequest类型异常和自定义消息

 public class HttpResponseBadRequestException: HttpResponseException
{
    /// <summary>
    /// Basic
    /// </summary>
    public HttpResponseBadRequestException()
        : this(string.Empty)
    {
    }

    /// <summary>
    /// Specific Constructor. Use this to send a Bad request exception
    /// with a custom message
    /// </summary>
    /// <param name="message">The message to be send in the response</param>
    public HttpResponseBadRequestException(string message)
        : base(new HttpResponseMessage(HttpStatusCode.BadRequest) {
            Content = new StringContent(message) })
    {
    }
}
公共类HttpResponseBadRequestException:HttpResponseException
{
/// 
///基本的
/// 
公共HttpResponseBadRequestException()
:this(string.Empty)
{
}
/// 
///特定构造函数。使用此命令可发送错误请求异常
///带有自定义消息
/// 
///要在响应中发送的消息
公共HttpResponseBadRequestException(字符串消息)
:base(新的HttpResponseMessage(HttpStatusCode.BadRequest){
Content=newstringcontent(message)})
{
}
}

如果要从HttpResponseException派生,则应使用类似于以下内容的代码:示例中是一个自定义异常,返回BadRequest类型异常和自定义消息

 public class HttpResponseBadRequestException: HttpResponseException
{
    /// <summary>
    /// Basic
    /// </summary>
    public HttpResponseBadRequestException()
        : this(string.Empty)
    {
    }

    /// <summary>
    /// Specific Constructor. Use this to send a Bad request exception
    /// with a custom message
    /// </summary>
    /// <param name="message">The message to be send in the response</param>
    public HttpResponseBadRequestException(string message)
        : base(new HttpResponseMessage(HttpStatusCode.BadRequest) {
            Content = new StringContent(message) })
    {
    }
}
公共类HttpResponseBadRequestException:HttpResponseException
{
/// 
///基本的
/// 
公共HttpResponseBadRequestException()
:this(string.Empty)
{
}
/// 
///特定构造函数。使用此命令可发送错误请求异常
///带有自定义消息
/// 
///要在响应中发送的消息
公共HttpResponseBadRequestException(字符串消息)
:base(新的HttpResponseMessage(HttpStatusCode.BadRequest){
Content=newstringcontent(message)})
{
}
}

你能提供一个关于如何扩展该类的示例吗?为什么需要扩展该类?现在我觉得我不需要它。你能提供一个关于如何扩展该类的示例吗?为什么需要扩展该类?现在我觉得我不需要它。这是一个很好的解决方案。任何在生产中使用此选项的人都应该添加一个检查,以确保原因字符串的长度不超过512个字符。这是一个很好的解决方案。任何在生产中使用此选项的人都应该添加一个检查,以确保原因字符串的长度不超过512个字符。