我有两个版本的网络表单。原文,在VB中。而新的,在C#。它们本质上都是相同的,但语言是不同的,而C#版本在功能方面和可读性方面略有改进。每个.aspx页面本身的差异通过@Page标记上的autoeventwireup属性而有所不同。现在,当我尝试运行VB版本时,我得到了我正在寻找的结果。 C#版本什么都不做。在每个页面的日志中,我看到VB版本正在从调用者那里获得一个POST,而C#版本正从调用者那里获得一个GET。因此,没有任何Request.Form值可用于C#版本。部署时的两个页面都由单独的Web应用程序调用。
VB Version page.aspx:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Custom Label Generator.aspx.vb" Inherits="_Default" %>
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
page #aspx的C#版本
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Custom Label Generator.aspx.cs" Inherits="CustomLabelGenerator.Custom_Label_Generator" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Custom Label Generator</title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
VB版代码隐藏:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'LogEvent("Request to print: " & Date.Today)
Try
If Request.Form.Count <= 0 Then
LogEvent("No label data was submitted with the request.")
Exit Sub
End If
'WRITE LABEL DATA BACK TO REQUESTING CLIENT
Me.Response.Clear()
Me.Response.ContentType = "text/plain"
Me.Response.BinaryWrite(System.Text.Encoding.Default.GetBytes(sLabelCode))
Me.Response.Flush()
Catch ex As Exception
LogEvent("Problem generating custom labels. More information: " + ex.Message)
End Try
End Sub
C#版本代码隐藏:
protected void Page_Load(object sender, EventArgs e)
{
LogHelper.LogEvent("Request to print at: " + DateTime.Now);
try
{
if(Request.Form.Count <= 0)
{
LogHelper.LogEvent("No label data was submitted with the request");
return;
}
Response.Clear();
Response.ContentType = "text/plain";
Response.BinaryWrite(Encoding.Default.GetBytes(labelCode));
Response.Flush();
}
catch (Exception ex)
{
LogHelper.LogEvent("Problem generating custom labels. More information :: " + ex.Message);
}
finally
{
LogHelper.CleanupOldLogs();
}
}
我目前在两者中都有一堆代码来吐出我能想到的所有请求信息。我也尝试撤消autoeventwireup,但在C#中似乎并不是一个可靠的等价物。我想知道这可能会更好地作为WebAPI呼叫,强迫它只接受POST,而不是乱搞任何其他kruft。我不确定我是否有时间进行这种转换,所以我们将非常感谢基于网络格式的解决方案!
更新:从VB版本转储Request.Form.Keys(减去我需要的 USEFUL 键)
9/23/2015 2:25:10 PM,ALL_HTTP => HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:1371
HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_EXPECT:100-continue
HTTP_HOST:localhost
9/23/2015 2:25:10 PM,ALL_RAW => Connection: Keep-Alive
Content-Length: 1371
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue
Host: localhost
9/23/2015 2:25:10 PM,APPL_MD_PATH => /LM/W3SVC/1/ROOT/Custom Label Generator
9/23/2015 2:25:10 PM,APPL_PHYSICAL_PATH => C:\Program Files (x86)\AgileElite\Projects Server\ContentLabelGenerator\
9/23/2015 2:25:10 PM,AUTH_TYPE =>
9/23/2015 2:25:10 PM,AUTH_USER =>
9/23/2015 2:25:10 PM,AUTH_PASSWORD =>
9/23/2015 2:25:10 PM,LOGON_USER =>
9/23/2015 2:25:10 PM,REMOTE_USER =>
9/23/2015 2:25:10 PM,CERT_COOKIE =>
9/23/2015 2:25:10 PM,CERT_FLAGS =>
9/23/2015 2:25:10 PM,CERT_ISSUER =>
9/23/2015 2:25:10 PM,CERT_KEYSIZE =>
9/23/2015 2:25:10 PM,CERT_SECRETKEYSIZE =>
9/23/2015 2:25:10 PM,CERT_SERIALNUMBER =>
9/23/2015 2:25:10 PM,CERT_SERVER_ISSUER =>
9/23/2015 2:25:10 PM,CERT_SERVER_SUBJECT =>
9/23/2015 2:25:10 PM,CERT_SUBJECT =>
9/23/2015 2:25:10 PM,CONTENT_LENGTH => 1371
9/23/2015 2:25:10 PM,CONTENT_TYPE => application/x-www-form-urlencoded
9/23/2015 2:25:10 PM,GATEWAY_INTERFACE => CGI/1.1
9/23/2015 2:25:10 PM,HTTPS => off
9/23/2015 2:25:10 PM,HTTPS_KEYSIZE =>
9/23/2015 2:25:10 PM,HTTPS_SECRETKEYSIZE =>
9/23/2015 2:25:10 PM,HTTPS_SERVER_ISSUER =>
9/23/2015 2:25:10 PM,HTTPS_SERVER_SUBJECT =>
9/23/2015 2:25:10 PM,INSTANCE_ID => 1
9/23/2015 2:25:10 PM,INSTANCE_META_PATH => /LM/W3SVC/1
9/23/2015 2:25:10 PM,LOCAL_ADDR => ::1
9/23/2015 2:25:10 PM,PATH_INFO => /Custom Label Generator/Custom Label Generator.aspx
9/23/2015 2:25:10 PM,PATH_TRANSLATED => C:\Program Files (x86)\AgileElite\Projects Server\ContentLabelGenerator\Custom Label Generator.aspx
9/23/2015 2:25:10 PM,QUERY_STRING =>
9/23/2015 2:25:10 PM,REMOTE_ADDR => ::1
9/23/2015 2:25:10 PM,REMOTE_HOST => ::1
9/23/2015 2:25:10 PM,REMOTE_PORT => 59112
9/23/2015 2:25:10 PM,**REQUEST_METHOD => POST**
9/23/2015 2:25:10 PM,SCRIPT_NAME => /Custom Label Generator/Custom Label Generator.aspx
9/23/2015 2:25:10 PM,SERVER_NAME => localhost
9/23/2015 2:25:10 PM,SERVER_PORT => 80
9/23/2015 2:25:10 PM,SERVER_PORT_SECURE => 0
9/23/2015 2:25:10 PM,SERVER_PROTOCOL => HTTP/1.1
9/23/2015 2:25:10 PM,SERVER_SOFTWARE => Microsoft-IIS/7.5
9/23/2015 2:25:10 PM,URL => /Custom Label Generator/Custom Label Generator.aspx
9/23/2015 2:25:10 PM,HTTP_CONNECTION => Keep-Alive
9/23/2015 2:25:10 PM,HTTP_CONTENT_LENGTH => 1371
9/23/2015 2:25:10 PM,HTTP_CONTENT_TYPE => application/x-www-form-urlencoded
9/23/2015 2:25:10 PM,HTTP_EXPECT => 100-continue
9/23/2015 2:25:10 PM,HTTP_HOST => localhost
Request.Params.Keys的C#版本转储(其他部分为空白)
9/23/2015 2:17:04 PM --- Request to print at: 9/23/2015 2:17:04 PM
9/23/2015 2:17:04 PM --- Request.Params.Keys:
9/23/2015 2:17:04 PM --- ALL_HTTP => HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_HOST:localhost
9/23/2015 2:17:04 PM --- ALL_RAW => Content-Type: application/x-www-form-urlencoded
Host: localhost
9/23/2015 2:17:04 PM --- APPL_MD_PATH => /LM/W3SVC/1/ROOT/Custom Label Generator
9/23/2015 2:17:04 PM --- APPL_PHYSICAL_PATH => C:\Program Files (x86)\AgileElite\Projects Server\ContentLabelGenerator\
9/23/2015 2:17:04 PM --- AUTH_TYPE =>
9/23/2015 2:17:04 PM --- AUTH_USER =>
9/23/2015 2:17:04 PM --- AUTH_PASSWORD =>
9/23/2015 2:17:05 PM --- LOGON_USER =>
9/23/2015 2:17:05 PM --- REMOTE_USER =>
9/23/2015 2:17:05 PM --- CERT_COOKIE =>
9/23/2015 2:17:05 PM --- CERT_FLAGS =>
9/23/2015 2:17:05 PM --- CERT_ISSUER =>
9/23/2015 2:17:05 PM --- CERT_KEYSIZE =>
9/23/2015 2:17:05 PM --- CERT_SECRETKEYSIZE =>
9/23/2015 2:17:05 PM --- CERT_SERIALNUMBER =>
9/23/2015 2:17:05 PM --- CERT_SERVER_ISSUER =>
9/23/2015 2:17:05 PM --- CERT_SERVER_SUBJECT =>
9/23/2015 2:17:05 PM --- CERT_SUBJECT =>
9/23/2015 2:17:05 PM --- CONTENT_LENGTH => 0
9/23/2015 2:17:05 PM --- CONTENT_TYPE => application/x-www-form-urlencoded
9/23/2015 2:17:05 PM --- GATEWAY_INTERFACE => CGI/1.1
9/23/2015 2:17:05 PM --- HTTPS => off
9/23/2015 2:17:05 PM --- HTTPS_KEYSIZE =>
9/23/2015 2:17:05 PM --- HTTPS_SECRETKEYSIZE =>
9/23/2015 2:17:05 PM --- HTTPS_SERVER_ISSUER =>
9/23/2015 2:17:05 PM --- HTTPS_SERVER_SUBJECT =>
9/23/2015 2:17:05 PM --- INSTANCE_ID => 1
9/23/2015 2:17:05 PM --- INSTANCE_META_PATH => /LM/W3SVC/1
9/23/2015 2:17:05 PM --- LOCAL_ADDR => ::1
9/23/2015 2:17:05 PM --- PATH_INFO => /Custom Label Generator/Custom Label Generator
9/23/2015 2:17:05 PM --- PATH_TRANSLATED => C:\Program Files (x86)\AgileElite\Projects Server\ContentLabelGenerator\Custom Label Generator
9/23/2015 2:17:05 PM --- QUERY_STRING =>
9/23/2015 2:17:05 PM --- REMOTE_ADDR => ::1
9/23/2015 2:17:05 PM --- REMOTE_HOST => ::1
9/23/2015 2:17:05 PM --- REMOTE_PORT => 58997
9/23/2015 2:17:05 PM --- **REQUEST_METHOD => GET**
9/23/2015 2:17:05 PM --- SCRIPT_NAME => /Custom Label Generator/Custom Label Generator
9/23/2015 2:17:05 PM --- SERVER_NAME => localhost
9/23/2015 2:17:05 PM --- SERVER_PORT => 80
9/23/2015 2:17:05 PM --- SERVER_PORT_SECURE => 0
9/23/2015 2:17:05 PM --- SERVER_PROTOCOL => HTTP/1.1
9/23/2015 2:17:05 PM --- SERVER_SOFTWARE => Microsoft-IIS/7.5
9/23/2015 2:17:05 PM --- URL => /Custom Label Generator/Custom Label Generator
9/23/2015 2:17:05 PM --- HTTP_CONTENT_TYPE => application/x-www-form-urlencoded
9/23/2015 2:17:05 PM --- HTTP_HOST => localhost
9/23/2015 2:17:05 PM --- /****************** Request.Form.Keys: ***********************/
9/23/2015 2:17:05 PM --- /****************** Request.Form.AllKeys: ***********************/
9/23/2015 2:17:05 PM --- /****************** Request.QueryString.Keys: ***********************/
答案 0 :(得分:5)
马特 我之前也遇到了'POST'神秘地改变为'GET'。由于在VS2013及更高版本中默认使用的FriendlyUrlSettings,它可能被重定向。
一种解决方法:转到RouteConfig.ResgisterRoutes中的App_Start文件夹并设置settings.AutoRedirectMode = RedirectMode.Off
public static class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Off;
routes.EnableFriendlyUrls(settings);
}
}
查看此链接以获取有关FriendlyUrls的更多信息:http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx
答案 1 :(得分:4)
马特,
此行为的原因是尝试减少提交多个表单的机会。这背后的理论被称为POST-REDIRECT-GET。来自https://en.wikipedia.org/wiki/Post/Redirect/Get:
当通过HTTP POST请求将Web表单提交给服务器时,尝试刷新某些用户代理中的服务器响应的Web用户可能会导致重新提交原始HTTP POST请求的内容,从而可能导致意外结果,例如重复购买网页。
为避免此问题,许多Web开发人员使用PRG模式1 - 而不是直接返回网页,POST操作返回重定向命令。 HTTP 1.1规范引入了HTTP 303(&#34;请参阅其他&#34;)响应代码,以确保在这种情况下,Web用户的浏览器可以安全地刷新服务器响应,而不会导致初始HTTP POST请求重新提交。但是,目前使用的最常见的商业应用程序(新旧相似)仍然会在这些情况下继续发出HTTP 302(&#34; Found&#34;)响应。
答案 2 :(得分:0)
Matt为什么不在表单中放一个方法=“POST”?
<form id="form1" runat="server" method="post">
答案 3 :(得分:0)
马特,
这是故意的吗?
VB 继承=&#34; _Default&#34;
C# 继承=&#34; CustomLabelGenerator.Custom_Label_Generator&#34;