[英]How to download multiple files? Getting “Server cannot clear headers after HTTP headers have been sent.”
[英]“Cannot redirect after HTTP headers have been sent.” When returning HttpResponseMessage with HttpStatusCode.Unauthorized
我正在使用新的Web Api測試版並希望返回
HttpResponseMessage<MyObject>(new MyObject{ MyMessage = "Go Away!" }, HttpStatusCode.Unauthorized)
來自我的一個ApiController動作。
表單身份驗證劫持響應,崩潰並添加錯誤"Cannot redirect after HTTP headers have been sent."
這是回復的HTML。
像這樣的常規抑制技術不適用於Web Api。
有人找到了解決這個問題的工作嗎?
我看過這個論壇帖子 ,人們報告同樣的問題,但那里的解決方案不適用於這種情況。 建議的第一個解決方案使用常規抑制技術,該技術不適用於web api。 第二個使用HttpMessageHandler在它到達控制器之前攔截請求,我希望控制器正常觸發。
在查看DelegatingHandler
我可以訪問HttpResponseMessage
但不知道如何處理它以阻止FormsAuth重定向。
使用Phil Haack的SuppressFormsAuthenticationRedirectModule時我遇到了同樣的問題
我設法通過清除服務器的錯誤來修復它,如下所示
private void OnEndRequest(object source, EventArgs args)
{
var context = (HttpApplication)source;
var response = context.Response;
if (context.Context.Items.Contains(SuppressAuthenticationKey))
{
context.Server.ClearError(); //Clearing server error
response.TrySkipIisCustomErrors = true;
response.ClearContent();
response.StatusCode = 401;
response.RedirectLocation = null;
}
}
嗨,因為這個問題似乎仍然是開放的我想我會指出最新的MVC 4 RC版本現在似乎默認禁用了表單重定向。 我做了一些挖掘,同時回復了這個問題, 阻止了阻止ASP.NET Web API響應的FormsAuthenticationModule
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.