Prevent aspx-page rendering

扶醉桌前 提交于 2019-12-24 19:44:44

问题


I got a "normal" ascx-Page which contains HTML-Parts as well as code behind. These elements should all be shown in normal conditions (works).

Now I want to be able to set a request-parameter which causes the page zu render differently. Then it sends the information on that page not human-readable but for a machine:

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

This code is inside the Page_PreRender. Now my problem is:The string is correctly sent to the browser but the "standard" html-content is still rendered after that.

When I remove the "Response.Close();" Comment I receive an "ERR_INVALID_RESPONSE"

Any clue how to solve this without creating an additional Page?


回答1:


Try adding Response.End()

Sends all currently buffered output to the client, stops execution of the page, and raises the EndRequest event.

Also, as @Richard said, add

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



回答2:


Can i suggest that Response.End() can throw an error.

Use Response.SuppressContent = true; to stop further processing of "standard" 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;
} 



回答3:


Have you tried setting the ContentType to application/json and End'ing the response, like so:

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

    return;
}    


来源:https://stackoverflow.com/questions/10980435/prevent-aspx-page-rendering

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!