Skip to main content
 首页 » 编程设计

C# 版本的 WebForms 页面不工作,VB 版本是

2024年01月18日33qq78292959

我有两个版本的网络表单。原始的,在 VB 中。而新的,在 C# 中。它们本质上都做同样的事情,但语言不同,C# 版本在功能和可读性方面有所改进。通过@Page 标记上的autoeventwireup 属性,每个.aspx 页面本身的差异是不同的。现在,当我尝试运行 VB 版本时,我得到了我正在寻找的结果。 C# 版本什么都不做。在每个页面的日志中,我看到 VB 版本正在从调用者那里获取 POST,而 C# 版本正在从调用者那里获取 GET。因此,C# 版本中没有任何 Request.Form 值可用。部署后的两个页面都由单独的 Web 应用程序调用。

VB版页面.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(减去我需要的有用的键)
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:  ***********************/ 

请您参考如下方法:

马特,
我以前也遇到过神秘地变成“GET”的“POST”。由于 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