[英]ASP.NET ActionResult gives different responses for localhost and Azure web service hosted
我有以下代碼
public ActionResult PerformMagic(string a, string b, int c)
{
try
{
// Some code which always gives an error and go to catch block
}
catch (Exception ex)
{
// ex.Message = "An error occured"
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return this.Content(System.Web.Helpers.Json.Encode(new { error = ex.Message }), "application/json");
}
}
所以調用返回以下結果,
{
config : {method: "GET", transformRequest: Array(1), transformResponse: Array(1), jsonpCallbackParam: "callback", paramSerializer: ƒ, …}
data :
error : "An error occured"
__proto__ : Object
headers : ƒ (name)
status : 400
statusText : ""
__proto__ : Object
}
因此,我獲取JSON中的data
,查找error
並顯示值( An error occured
)作為警報。
這在localhost中運行時非常有效,但在將其部署到Azure App服務並運行時,響應如下所示
{
config : {method: "GET", transformRequest: Array(1), transformResponse: Array(1), jsonpCallbackParam: "callback", paramSerializer: ƒ, …}
data : "Bad Request"
headers : ƒ (name)
status : 400
statusText : "Bad Request"
__proto__ : Object
}
這意味着,我無法在data
找到error
。 任何人都可以解釋我為什么會這樣嗎?
事實證明,其原因在於httpErrors元素 。 與本地計算機上的行為相比,我可以對Azure上具有不同默認行為的元素進行映像。
簡而言之:您可以通過在web.config中的system.WebServer
元素下添加它來解決它:
<httpErrors existingResponse="PassThrough" />
可能的值有Auto(0),Replace(1)和PassThrough(2):
我不完全確定這種變化對安全的影響。 但它確實允許您向可能不屬於那里的用戶返回(例外)詳細信息。
答案基於這篇文章: ASP.NET + Azure 400錯誤請求不返回JSON數據
確保兩台計算機(localhost和azure)運行相同的.NET Framework。 否則,檢查NuGet包中處理序列化的任何奇怪的緩存。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.