我得到了一个“普通”的ascx页,其中包含HTML部分以及后面的代码。这些元素都应在正常状态下显示(工作)。
现在,我希望能够设置一个导致页面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”
任何线索,如何在不创建额外页面的情况下解决此问题?
尝试添加Response.End()
我可以建议Response.End()会抛出错误。
您是否尝试过将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;
}
设置为ContentType
,并且将application/json
设置为响应,例如:
即使是正确行为也为string jsonProperty = Request["JSonProperty"];
if (!string.IsNullOrEmpty(jsonProperty))
{
Response.Clear();
Response.ContentType = "application/json";
Response.Write(RenderJSon());
Response.End();
return;
}
。这是因为提供该方法是为了与旧ASP向后兼容,而不是为了支持性能而设计的。为了防止进一步的处理,它抛出recommended to avoid calling Response.End()
,破坏线程,迫使系统稍后在处理新请求时启动一个新线程,而不是将该线程返回到ASP.NET线程池。建议停止ASP.NET管道的进一步处理的替代方法是调用Response.End()
并立即返回。但是,这不会终止ASP.NET页的处理。