C# Web Api异常处理
在我的应用程序中,我尝试使用异常处理。我试图做的是创建一个单独的类,并从HttpResponseException扩展它 这是我一直写到现在的代码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 {
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个字符。