Prevent aspx-page rendering

2019-09-09 12: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?

3条回答
冷血范
2楼-- · 2019-09-09 12:59

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";
查看更多
萌系小妹纸
3楼-- · 2019-09-09 13:07

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;
} 
查看更多
我命由我不由天
4楼-- · 2019-09-09 13:07

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;
}    
查看更多
登录 后发表回答