2
當用戶沒有執行特定任務的權限時,我想從我的Asp.Net WebApi控制器返回Http 403錯誤。從Asp.Net WebApi控制器返回HTTP 403子狀態
但是,我想在此上使用substatus以提供有關錯誤的更多詳細信息以及錯誤消息。
目前,我所得到的是
HTTP/1.1 403 Read access forbidden
,但我想看到的是
HTTP/1.1 403.2 Read access forbidden
我目前使用的代碼:
[HttpGet]
public EnrollmentDetail Details(int id)
{
var enrollmentDetail = _context.GetEnrollmentDetail(id);
if (!enrollmentDetail.R)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden)
{
ReasonPhrase = "Read access forbidden"
});
}
return enrollmentDetail;
}
我無法找到任何有關如何將這些子狀態添加到響應的信息。有沒有什麼辦法可以用內置的類來完成?如果沒有,有沒有辦法寫一個自定義HttpException
這可以爲我做這個?
我無法找到確切的答案,但我認爲這兩個鏈接應該有所幫助。 http://www.iis.net/learn/troubleshoot/diagnosing-http-errors/how-to-use-http-detailed-errors-in-iis http://msdn.microsoft.com/en-us /library/system.web.httpresponse.substatuscode.aspx –