2012-06-11 122 views
0

我得到了一個「正常」的ascx頁面,其中包含HTML部分以及代碼隱藏。這些元素都應在正常條件下顯示(工作)。防止aspx頁面呈現

現在我想能夠設置一個請求參數,導致頁面zu呈現不同。然後它發送該頁面上的信息不是人類可讀的,但對於一臺機器:

string jsonProperty = Request["JSonProperty"]; 
       if (!string.IsNullOrEmpty(jsonProperty)) 
       {      
        Response.Clear(); 
        Response.Write(RenderJSon()); 
        // Response.Close(); 
        return; 

此代碼位於Page_PreRender中。 現在我的問題是:該字符串被正確發送到瀏覽器,但「標準」HTML內容仍然呈現之後。

當我刪除「Response.Close();」評論我收到「ERR_INVALID_RESPONSE」

任何線索如何解決這個問題,而不創建一個額外的頁面?

回答

2

嘗試增加Response.End()

發送所有當前緩衝輸出到客戶端,停止頁面的執行,並引發EndRequest事件。

而且,@Richard說,加

context.Response.ContentType = "application/json"; 
1

你有沒有嘗試設置ContentTypeapplication/jsonEnd「荷蘭國際集團的反應,就像這樣:

string jsonProperty = Request["JSonProperty"]; 
if (!string.IsNullOrEmpty(jsonProperty)) 
{      
    Response.Clear(); 
    Response.ContentType = "application/json"; 
    Response.Write(RenderJSon()); 
    Response.End(); 

    return; 
}  
6

我可以建議Response.End() 可能會引發錯誤。

使用Response.SuppressContent = true;停止「標準」的進一步處理html

string jsonProperty = Request["JSonProperty"]; 
if (!string.IsNullOrEmpty(jsonProperty)) 
{      
    Response.Clear(); 
    Response.ContentType = "application/json"; 
    Response.Write(RenderJSon()); 

    Response.Flush();     // Flush the data to browser 
    Response.SuppressContent = true;  // Suppress further output - "standard" html- 
             // content is not rendered after this 

    return; 
}