請問這兩個功能有何差異,感覺只是差在T value
public static HttpResponseMessage CreateErrorResponse(this HttpRequestMessage request, HttpStatusCode statusCode, string message);
public static HttpResponseMessage CreateResponse(this HttpRequestMessage request, HttpStatusCode statusCode, T value);
但如果我想傳的錯誤是一個類別或物件進去,CreateErrorResponse好像沒有,只能string message
請求失敗()
{
"code" : "400",
"message" : "自定"message",
"detail" : "自定detail",
"data" : "none"
}
簡單說
一般/正常 HTTP Response 用 CreateResponse
異常 HTTP Response 用 CreateErrorResponse
CreateResponse
CreateResponse is a Helper method that performs content negotiation and creates a HttpResponseMessage with an instance of System.Net.Http.ObjectContent`1 as the content and OK as the status code if a formatter can be found. If no formatter is found, this method returns a response with status 406 NotAcceptable.
CreateErrorResponse
CreateErrorResponse also a Helper method that creates a HttpResponseMessage representing an error with an instance of System.Net.Http.ObjectContent`1 wrapping an HttpError for model state modelState. If no formatter is found, this method returns a response with status 406 NotAcceptable
When to Use:
Usually when your request would have successful response then you have to use CreateResponsewith http status code. on the other hand if your request encounters a server side exception its good to handle with CreateErrorResponse with relevant error code.