1
我有以下服務方法,在給出以下寧靜url時調用http://xxxxxxxxx/api/checkemail?email={email}
如果電子郵件的長度超過254個字符,則我想要拋出一個參數超出範圍的異常,這可以正常工作,並覆蓋不起作用的http響應狀態碼。HttpResponse在設置爲自定義值之後返回狀態代碼400
當我在fiddler中執行url而不是返回狀態碼94043(我想使用的一個隨機數)時,它返回了400個錯誤的請求。任何想法爲什麼?
public bool CheckIfUserExists(string email)
{
if (email.Length > 254)
{
HttpContext.Current.Response.StatusCode = 94043;
HttpContext.Current.Response.Status = "The email exceeds the maximum character limit";
throw new ArgumentOutOfRangeException("Email", "The email address is greater than 254 characters");
}
return CheckIfUserExists(email);
}
它可能不會讓你因爲你不應該發明你自己的狀態代碼而不是Http規範的一部分,我猜這個框架會強制執行它。一般來說,4xx意味着某種用戶錯誤,因此您應該返回400個錯誤請求或其他4xx錯誤代碼 – Ben