[ASP.net Web Form] 查看Request資訊
最近論壇上有人發問
1.怎麼判斷使用者是不是按F5重新整理Postback(重覆交易問題)
因為自己也有此需求,所以紀錄一下觀察結果
(要用HttpWatch軟體看也是一種選擇,但看得到總該想辦法取到值來判斷)
測試環境:Windows 7 家用進階版 + IE8瀏覽器 + 把Web Site掛到本機的IIS7.5(應用程式集區ASP.NET v4.0 整合式)
看表格時,須搭配MSDN文件HttpRequest.ServerVariables屬性的IIS Server Variables瞭解各Header Key值代表什麼意思
(簡體中文版:HttpRequest.ServerVariables[]中的参数集 - 花香的蜂 - 博客园)
程式碼:
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HttpRequestWatcher.aspx.cs" Inherits="HttpRequestWatcher" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function jQueryGet() {
$.get("HttpRequestWatcher.aspx", { data: "passQueryString" }, jqueryCallHandler);
}
function jQueryPost() {
$.post("HttpRequestWatcher.aspx", { data: "passFormData" }, jqueryCallHandler);
}
function jqueryCallHandler(htmlVal) {
$("#div_jQueryAjax").html(htmlVal);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager runat="server" /><br />
Page_Load回傳的字串:<br />
<asp:Literal id="li_pageLoadText" runat="server" /><br />
<asp:Button Text="按鈕PostBack" ID="btn_Go" runat="server" /><br />
<hr />
jQuery區:<br />
<a id="a_jQueryGet" href="javascript:jQueryGet();" >jQueryAjaxGet</a><br />
<a id="a_jQueryPost" href="javascript:jQueryPost();" >jQueryAjaxPost</a><br />
jQuery回傳的HTML:<br />
<div id="div_jQueryAjax">
</div>
<hr />
ASP.net UpdatePanel區:<br />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button Text="按鈕Updatepanel" ID="btn_UpdatePanel" runat="server"
onclick="btn_UpdatePanel_Click" /><br />
<asp:Literal ID="li_Text" runat="server" /><br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_UpdatePanel" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
</form>
</body>
</html>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
public partial class HttpRequestWatcher : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' border='1' cellpadding='0' cellspacing='0'>");
foreach (string key in Request.ServerVariables.AllKeys)
{
sb.Append("<tr>");
sb.Append("<td>" + key + "</td>" + "<td>" + Request.ServerVariables[key] + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
li_pageLoadText.Text = sb.ToString();
}
protected void btn_UpdatePanel_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
sb.Append("<table width='100%' border='1' cellpadding='0' cellspacing='0'>");
foreach (string key in Request.ServerVariables.AllKeys)
{
sb.Append("<tr>");
sb.Append("<td>" + key + "</td>" + "<td>" + Request.ServerVariables[key] + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
li_Text.Text = sb.ToString();
}
}
發出的要求網址皆為:http://localhost/MyHttpWatch/HttpRequestWatcher.aspx
Case 1:網頁第一次執行(Get Method)的畫面: