1. 程式人生 > >Context.Response.End(); VS HttpContext.Current.ApplicationInstance.CompleteRequest();

Context.Response.End(); VS HttpContext.Current.ApplicationInstance.CompleteRequest();

今天遇到一個問題,頁面Client端send一個ajax請求,然後在server端返回一個json的字串

        $.ajax({
                url: "xxxxx.aspx",
                type: "post",
                data: { action: "xxx", Id: $('#hiddenId').val(), fileKey: $('.hiddenFileKey').val(), tableName: selectedTableName }
            })
            .done(
function (data) { processResponse(data, false); }) .fail(function (jqXHR, textStatus, errorThrown) { alert(errorThrown); });

功能很簡單,可是因為 Context.Response.End(); 的一慣壞名聲(強制返回),在server端不能使用,於是隻能改為使用HttpContext.Current.ApplicationInstance.CompleteRequest(); 本來也沒有什麼問題。

                    Context.Response.Clear();
                    Context.Response.ContentType = "application/json";
                    Context.Response.AddHeader("content-length", ResponseJsonStr.Length.ToString());
                    Context.Response.Write(ResponseJsonStr);//Context.Response.End();
ApplicationInstance.CompleteRequest(); return;

就是上面的程式碼,卻出現了一個奇怪的問題(目前為止不清楚問題的原因,同樣的程式碼在其他頁面可以正常使用),返回到client頁面的json string除了json data的部分之外,還包含了整個頁面的html source code. 導致json parse時出現error

JSON.parse: unexpected non-whitespace character after JSON data

無奈之下,去網上尋求解決辦法,發現解法都是要使用 Context.Response.End();。TT

終於在檢視關於Context.Response的文件時發現一個property: SuppressContent,可以控制內容是否回傳client 端。

於是將server端程式碼改為

                    Context.Response.Clear();
                    Context.Response.ContentType = "application/json";
                    Context.Response.AddHeader("content-length", ResponseJsonStr.Length.ToString());
                    Context.Response.Write(ResponseJsonStr);
                    Context.Response.Flush(); // Sends all currently buffered output to the client.
                    //Context.Response.End();
                    Response.SuppressContent = true; // Gets or sets a value indicating whether to send HTTP content to the client.
                    ApplicationInstance.CompleteRequest();
                    return;

問題解決。

相關推薦

Context.Response.End(); VS HttpContext.Current.ApplicationInstance.CompleteRequest();

今天遇到一個問題,頁面Client端send一個ajax請求,然後在server端返回一個json的字串 $.ajax({ url: "xxxxx.aspx", type: "post",

try裏Response.end()問題

abort con mic exceptio tps font stack src -a 問題 在xxx.aspx.cs中處理異步請求,大致代碼如下: 但會發現始終會進catch。 原因 Response.End()會引發ThreadAbortException。

MVC繞過登陸界面驗證時HttpContext.Current.User.Identity.Name取值為空問題解決方法

isnull auth asa ren lob obj req ret 方法 Global.asax界面添加如下方法: void FormsAuthentication_Authenticate(object sender, FormsAuthenticationEven

HttpContext.Current.Session[strName]未將對象引用設置到對象的實例

-a work -1 run ima frame tar -h ons 項目開發是在4.5.1上,不知道為啥客戶提供的服務器上安裝的是4.5,差別不大也沒去升級,然後部署MVC的時候web.config報錯 <system.web> <compi

node.js中的http.response.end方法使用說明

-c 方法 net ron blog 分享 函數 客戶端 pan 轉載自:http://m.jb51.net/article/58468.htm 本文介紹了http.response.end的方法說明、語法、接收參數、使用實例和實現源碼,需要的朋友可以參考下 方法

HttpContext.Current.Session 和 Session 的區別

使用 pro 啟動 protect and con 動作 system ont Session(會話)通常指一個動作從開始到結束不間斷的一個動作。 例如“打電話”,通常是“1.拿起電話--2.撥對方號碼--3.對方截圖--4.掛機&rd

接收客戶端在HTTP請求期間發送過來的值(HttpRequestBase/HttpRequest、HttpContextBase/HttpConext、HttpContext.Current

實例 tro 註意 quest 內部 存儲 信息 post span 雖然基本知道Asp.Net MVC程序如何接收來自客戶端HTTP的信息,但是關於HTTP的請求流轉和數據傳遞機制有些模糊,今天就來稍作整理吧。 來看看客戶端代碼: <form action="De

HttpContext.Current.Server.MapPath("/") 未將對象設置到對象的實例異常。

manage bstr director pub tpc 引用 對象引用 失效 settings 多線程中的System.Web.HttpContext.Current.Server.MapPath("/") 多線程中Server.MapPath會失效。。。 網上找到幾

.Net HttpContext.Current.Request 常用處理方案

val 常用 all setvalue ont collect n) readonly prop 1、清理request的請求數據 PropertyInfo isreadonly =typeof(System.Collections.Specialized.NameVa

ASP.NET多線程下使用HttpContext.Current為null解決方案

sdn lower null com 獲取文件 .html 模擬 public 一個 問題一:多線程下獲取文件絕對路徑 問題一:多線程下獲取文件絕對路徑 當我們使用HttpContext.Current.Server.MapPath(strPath)獲取絕對路徑時H

HttpContext.Current並非無處不在

sta XML tostring sys 設計 system code context .net 無處不在的HttpContext 由於ASP.NET提供了靜態屬性HttpContext.Current,因此獲取HttpContext對象就非常方便了。也正是因為這個原因,所

HttpContext.Current.Server未將對象引用到實例

map IT lse 代碼 ont XML cep try cit 問題描述: 在一些類庫中需要讀取當前系統的xml文件,當時用HttpContext.Current無法找到實例化對象 解決代碼如下: XmlDocument xml = new XmlDocument

HttpContext.Current.Cache 過期時間

dds ceo close logutil 依賴項 rop exp ons valid 為了更快的讀取數據,我們一般會把常用到的數據加載到Cache中 在.NET中,Cache的存在可以依賴多中方式,主要用到HttpContext.Current.Cache類 在這裏,我主

【轉】在.net Core 中像以前那樣的使用HttpContext.Current

single .get soft ESS contex vid pro del 文件中 1.首先我們要創建一個靜態類 public static class MyHttpContext { public static IServic

Response.End方法 在try...catch語句中執行Response.End()後如何停止執行catch語句中的內容

  文章:在try...catch語句中執行Response.End()後如何停止執行catch語句中的內容 呼叫Response.End()方法能保證,只輸出End方法之前的內容。 呼叫Context.ApplicationInstance.CompleteRequest();方法,會輸出方

HttpContext.Current.Request.Url 地址:獲取域名

ont tail www. 技術 absolute url art blog raw 假設當前頁完整地址是:http://www.test.com/aaa/bbb.aspx?id=5&name=kelli 協議名----http://域名 ---- www.tes

HttpContext.Current.Request.ServerVariables 所有命名的伺服器變數的名稱和值

protected void Page_Load(object sender, EventArgs e)    {        int loop1, loop2;        NameValueCollection coll;         // Load Server

非同步任務,HttpContext.Current為null解決辦法

最近在開發一個後臺管理系統專案,為了提高登入的速度,就把記錄登入日誌放到一個非同步任務裡面。 Action taskAction = () => { SaveLog(); }; Task task = new Task(taskAction); task.Start();

異步任務,HttpContext.Current為null解決辦法

clas tpc 異步 set tar 本地 獲取 pre ren 最近在開發一個後臺管理系統項目,為了提高登錄的速度,就把記錄登錄日誌放到一個異步任務裏面。 Action taskAction = () => { SaveLog(); }; T

HttpContext.Current 屬性 ---為當前 HTTP 請求獲取或設定 HttpContext 物件。

protected void Page_Load(object sender, EventArgs e) { HttpContext context = HttpContext.Current; Response.Write("<p>HttpCon