2019年7月5日 星期五

webmethod 回傳錯誤

var httpContext=System.Web.HttpContext.Current;
httpContext.Response.Write(錯誤訊息);
httpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
httpContext.Response.Flush();
httpContext.Response.SuppressContent = true;
httpContext.ApplicationInstance.CompleteRequest();

※若為 async 函數,await 後就無法再取得 System.Web.HttpContext.Current,必須先用變數記錄起來,await 之後再填回
var httpcontext = HttpContext.Current;
await xxxAsync().ConfigureAwait(false);
HttpContext.Current = httpcontext;

沒有留言:

自訂權限驗證機制

// 使用 filter [Route("api/[controller]")] [ApiController] [Authorize] [TypeFilter(typeof(CustomAsyncAuthorizationFilter))] public c...