====
try{
...
} catch (Exception ex)
{
Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
Response.Write(ex.詳細訊息());
return null;
}
public static string 詳細訊息(this Exception ex)
{
if (ex is System.Data.Entity.Validation.DbEntityValidationException)
return ((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors.SelectMany(a => a.ValidationErrors.Select(b => b.ErrorMessage)).Join("\n");
else
return ex.InnerException?.InnerException?.Message ?? ex.InnerException?.Message ?? ex.Message;
}
javascript
====
$http.post('@Url.Action("匯出")')
.then(function (response) {
...
}, function (response) {
// response.data = ex.詳細訊息()
});
注意 : web.config 需加入以下設定,否則 response.data 會固定為 iis 針對Response.StatusCode預設的資料
<system.webServer>
<httpErrors existingResponse="PassThrough"></httpErrors>
asp.net webmethod 寫法
====
HttpContext.Current.Response.Write(ex.Message);
HttpContext.Current.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
HttpContext.Current.Response.Flush();
//忽視之後透過Response.Write輸出的內容
HttpContext.Current.Response.SuppressContent = true;
//忽略之後ASP.NET Pipeline的處理步驟,直接跳關到EndRequest
HttpContext.Current.ApplicationInstance.CompleteRequest();
沒有留言:
張貼留言