Cor*_*rey 5 c# asp.net request.form
我在这里学习 ASP.NET 这就是为什么我显然在这里问一个问题。因此,当我在<% %>分隔符内运行语句时,一切正常。我尝试改为在 a 中运行它<script runat="server">,但它不起作用。我只是很好奇这两种方法之间有什么如此显着的不同。我想使用脚本方法,但它只适用于<% %>.
我的例子是...从 POST 方法中获取“userInput”(一个数字)的基本标准形式。
<form action="calcprime.aspx" method="post">
Enter a number between 1-999:
<input type="text" name="userInput" maxlength="3" />
<input type="submit" name="submit" value="Submit" />
</form>
Run Code Online (Sandbox Code Playgroud)
然后,如果我需要将字符串转换为整数以对其进行数学运算,我会这样做...
<%@ Page Language="C#" %>
<script runat="server">
int num = int.Parse(Request.Form["userInput"]);
</script>
<%=num%> // <-should display the number in theory..(in my head)
Run Code Online (Sandbox Code Playgroud)
不幸的是,上面的代码错误并且对我不起作用,但是仅<% %>使用完全相同的代码方法的替代方法可以 100% 正常工作。比如下面...
<%@ Page Language="C#" %>
<%
int num = int.Parse(Request.Form["userInput"]);
Response.Write(num); //displays the number as it should.. 100% working
%>
Run Code Online (Sandbox Code Playgroud)
所以我的问题是。为什么脚本方式不起作用?不是一样的吗?使用 C# 在 ASP.NET 中处理这个基本场景的正确方法是什么?我的方法是否实用,或者是否有我应该注意的标准?该数字将对其进行数学运算,这就是我需要将其转换为整数的原因。这只是一些基本的基础知识,我觉得在学习不良做法之前我应该知道正确的方法。
<script runat="server">
int num = 1;
</script>
<%=num%>
Run Code Online (Sandbox Code Playgroud)
这在我的机器上运行良好。我1在我的页面上看到了。
但是,这不起作用:
<script runat="server">
int num = int.Parse(Request.Form["userInput"]);
</script>
<%=num%>
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
CS0120:非静态字段、方法或属性“System.Web.UI.Page.Request.get”需要对象引用
我怀疑您也遇到了该错误,但没有将其包含在您的问题中。请注意,如果您的代码出现错误,请将其包含在您的问题中。不要假设我们知道您有错误。
这有效,假设我将查询字符串适当地添加到请求 URL:
<script runat="server">
int num = int.Parse(HttpContext.Current.Request.QueryString["randomnum"].ToString());
</script>
<%=num%>
Run Code Online (Sandbox Code Playgroud)
我怀疑这也会起作用,假设您已经将表单值发布到页面。但是,你的问题不完整,所以我不知道你是否做到了。这只是为了说明,您需要提交一个Minimal、Verifiable 和 Complete 示例。
<script runat="server">
int num = int.Parse(HttpContext.Current.Request.Form["userInput"]);
</script>
<%=num%>
Run Code Online (Sandbox Code Playgroud)
然而,最后,您可能不应该在您的页面上内嵌代码块(无论是使用脚本标记还是内联表达式)。这在后面的代码上处理得更好。<% %>stuff 在某些情况下很好,但您通常应该只使用它来将值注入页面,例如,当您在转发器中评估某些内容时。如果您发现自己做了很多内联表达式,您可能会考虑切换到 ASP.NET MVC 或网页。两者都使用Razor 视图引擎,它更干净。
您的.aspx文件.cs由幕后的 ASP.NET 编译器(独立于 IDE 中的 C# 编译器)转换为文件(即纯 C#)。.cs如果您Temporary ASP.NET Files在计算机上找到“ ”文件夹,就可以看到这些临时文件。
这些.cs文件中的类有一个名为的大函数Render,它是响应流的输出(使用Response.Write)。文件中的所有静态 HTML 都.aspx被转换为一个String实例并直接输入到Response.Write.
<% %>块被转换为内联 C# 代码,这些代码String在Render函数内部分解了这些大量实例。
<script runat="server"> 块被粘贴为这个运行时生成的类中的类成员。
<asp:Foo runat="server">元素被转换为Control实例化和渲染调用。
所以这:
<%@ Page Inherits="ParentPageClass" %>
<html>
<head>
<script runat="server">
String DoSomething() {
return "lulz";
}
</script>
</head>
<body>
<% int x = 5; %>
<%= x %>
<div>
<asp:Button runat="server" />
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
转换成这样:
(我通过删除额外的跟踪调用简化了这个例子。#line文本是特殊的预处理器指令,因此 YSOD 系统可以将运行时错误映射回您的.aspx文件)。
namespace ASP {
[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
public class webform1_aspx : ParentPageClass, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
String DoSomething() {
return "lulz";
}
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
private global::System.Web.UI.WebControls.HyperLink @__BuildControl__control2() {
global::System.Web.UI.WebControls.HyperLink @__ctrl;
@__ctrl = new global::System.Web.UI.WebControls.HyperLink();
@__ctrl.ApplyStyleSheetSkin(this);
@__ctrl.Text = "WebControls are evil";
}
[System.Diagnostics.DebuggerNonUserCodeAttribute()]
private void @__BuildControlTree(webform1_aspx @__ctrl) {
this.InitializeCulture();
global::System.Web.UI.WebControls.HyperLink @__ctrl1;
@__ctrl1 = this.@__BuildControl__control2();
System.Web.UI.IParserAccessor @__parser = ((System.Web.UI.IParserAccessor)(@__ctrl));
@__parser.AddParsedSubObject(@__ctrl1);
@__ctrl.SetRenderMethodDelegate(new System.Web.UI.RenderMethod(this.@__Render__control1));
}
private void @__Render__control1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
@__w.Write("\r\n </head>\r\n <body>\r\n ");
#line 11 "c:\users\dai\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\WebForm1.aspx"
int x = 5;
#line default
#line hidden
#line 12 "c:\users\dai\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\WebForm1.aspx"
@__w.Write( x );
#line default
#line hidden
@__w.Write("\r\n <div>\r\n ");
parameterContainer.Controls[0].RenderControl(@__w);
@__w.Write("\r\n </div>\r\n </body>\r\n </html>");
}
}
}
Run Code Online (Sandbox Code Playgroud)