When should I set HttpResponse.SuppressContent to true(何时应将HttpResponse.SuppressContent设置为True)
问题描述
我编写了用于验证url的HTTP模块,如果URL包含特殊符号,我将重定向到根目录。
我找到了不同的示例,有人建议将HttpResponse.SuppressContent属性设置为True。但我不确定在这种情况下会发生什么。MSDN表示它正在指示是否向客户端发送HTTP内容,这是否意味着重定向不会由客户端发起,而是由服务器发起?
推荐答案
默认情况下,ASP.Net缓冲处理程序的输出。Response.SupressContent阻止将该内容发送到客户端,但仍将发送标头。
示例1-带缓冲和SupressContent=FALSE的输出
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("No Supression");
}
</script>
Dynamic Content
<%= DateTime.Now %>
原始http响应:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
Vary: Accept-Encoding
Date: Sun, 03 May 2015 23:29:17 GMT
Content-Length: 44
No Supression
Dynamic Content
5/3/2015 5:29:17 PM
示例2-带缓冲和SupressContent=True的输出
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Suppress it all!");
Response.SuppressContent = true;
}
</script>
Dynamic Content
<%= DateTime.Now %>
原始http响应:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html
Date: Sun, 03 May 2015 23:34:13 GMT
Content-Length: 0
请注意,此输出没有内容。
您的问题具体是关于重定向会发生什么。
示例3-SupressContent=True时重定向
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("~/SomewhereElse.aspx");
// Exception was thrown by previous statement. These two lines do not run.
Response.Write("Redirect Supress Content");
Response.SuppressContent = true;
}
</script>
Dynamic Content
<%= DateTime.Now %>
原始http响应:
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: /SomewhereElse.aspx
Date: Sun, 03 May 2015 23:35:40 GMT
Content-Length: 136
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/SomewhereElse.aspx">here</a>.</h2>
</body></html>
注意仍然有一具身体。这是因为我们没有允许请求完全处理。默认情况下,Response.ReDirect将引发异常,并且Response.SupressContent属性未设置为True。如果我们将False传递给第二个参数,则它类似于上面的示例。
示例4-当SupressContent=True时重定向,并且Response.ReDirect不引发异常。
<%@ Page Language="C#" AutoEventWireup="true" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("~/SomewhereElse.aspx", false);
Response.Write("Redirect Supress Content");
Response.SuppressContent = true;
}
</script>
Dynamic Content
<%= DateTime.Now %>
原始http响应:
HTTP/1.1 302 Found
Cache-Control: private
Content-Type: text/html
Location: /SomewhereElse.aspx
Date: Sun, 03 May 2015 23:37:45 GMT
Content-Length: 0
这篇关于何时应将HttpResponse.SuppressContent设置为True的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:何时应将HttpResponse.SuppressContent设置为True
基础教程推荐
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
