ASP.NET如何获取url参数

c#的文件,我传递一个参数pname到一页,然后这一页的一个文本框
txtPName想获取前一页传递过来的url参数pname,应该如何写?是要在.aspx.cs文件里面写的吗??请说出具体应该如何做?谢谢!

正确的方法是:HttpContext.Current.Request.Url.PathAndQuery
Request.Url.PathAndQuery
string 类型。等于 Request.Url.AbsolutePath 和 Request.Url.Query 相加。
Request.Url.AbsolutePath
string 类型。指当前页面 URL 的绝对路径,不包括查询字符串部分。
示例:/test/Default.aspx
Request.Url.Query
string 类型。指当前页面 URL 的查询字符串,从“?”开始。
示例:?a=1&b=2

1、通过ASP.NET获取
如果测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:
Request.ApplicationPath: /testweb
Request.CurrentExecutionFilePath: /testweb/default.aspx
Request.FilePath: /testweb/default.aspx
Request.Path: /testweb/default.aspx
Request.PhysicalApplicationPath: E:\WWW\testwebRequest.PhysicalPath: E:\WWW\testweb\default.aspx
Request.RawUrl: /testweb/default.aspx
Request.Url.AbsolutePath: /testweb/default.aspx
Request.Url.AbsoluteUrl: http://www.test.com/testweb/default.aspx
Request.Url.Host: www.test.com
Request.Url.LocalPath: /testweb/default.aspx

URL http://localhost:1897/News/Press/Content.aspx/123?id=1#toc
Request.ApplicationPath /
Request.PhysicalPath D:\Projects\Solution\web\News\Press\Content.aspx
System.IO.Path.GetDirectoryName(Request.PhysicalPath) D:\Projects\Solution\web\News\Press
Request.PhysicalApplicationPath D:\Projects\Solution\web\
System.IO.Path.GetFileName(Request.PhysicalPath) Content.aspx
Request.CurrentExecutionFilePath /News/Press/Content.aspx
Request.FilePath /News/Press/Content.aspx
Request.Path /News/Press/Content.aspx/123
Request.RawUrl /News/Press/Content.aspx/123?id=1
Request.Url.AbsolutePath /News/Press/Content.aspx/123
Request.Url.AbsoluteUri http://localhost:1897/News/Press/Content.aspx/123?id=1
Request.Url.Scheme http
Request.Url.Host localhost
Request.Url.Port 1897
Request.Url.Authority localhost:1897
Request.Url.LocalPath /News/Press/Content.aspx/123
Request.PathInfo /123
Request.Url.PathAndQuery /News/Press/Content.aspx/123?id=1
Request.Url.Query ?id=1
Request.Url.Fragment
Request.Url.Segments
/
News/
Press/
Content.aspx/
123

2、通过JS获取
<table width=100% cellpadding=0 cellspacing=0 border=0 >
<script>
thisURL = document.URL;
thisHREF = document.location.href;
thisSLoc = self.location.href;
thisDLoc = document.location;
strwrite = "<tr><td valign=top>thisURL: </td><td>[" + thisURL + "]</td></tr>"
strwrite += "<tr><td valign=top>thisHREF: </td><td>[" + thisHREF + "]</td></tr>"
strwrite += "<tr><td valign=top>thisSLoc: </td><td>[" + thisSLoc + "]</td></tr>"
strwrite += "<tr><td valign=top>thisDLoc: </td><td>[" + thisDLoc + "]</td></tr>"
document.write( strwrite );
</script>
thisDLoc = document.location; <BR>
thisURL = document.URL; <BR>
thisHREF = document.location.href; <BR>
thisSLoc = self.location.href;<BR>
<script>
thisTLoc = top.location.href;
thisPLoc = parent.document.location;
thisTHost = top.location.hostname;
thisHost = location.hostname;
strwrite = "<tr><td valign=top>thisTLoc: </td><td>[" + thisTLoc + "]</td></tr>"
strwrite += "<tr><td valign=top>thisPLoc: </td><td>[" + thisPLoc + "]</td></tr>"
strwrite += "<tr><td valign=top>thisTHost: </td><td>[" + thisTHost + "]</td></tr>"
strwrite += "<tr><td valign=top>thisHost: </td><td>[" + thisHost + "]</td></tr>"
document.write( strwrite );
</script>
thisTLoc = top.location.href; <BR>
thisPLoc = parent.document.location; <BR>
thisTHost = top.location.hostname; <BR>
thisHost = location.hostname;<BR>
<script>
tmpHPage = thisHREF.split( "/" );
thisHPage = tmpHPage[ tmpHPage.length-1 ];
tmpUPage = thisURL.split( "/" );
thisUPage = tmpUPage[ tmpUPage.length-1 ];
strwrite = "<tr><td valign=top>thisHPage: </td><td>[" + thisHPage + "]</td></tr>"
strwrite += "<tr><td valign=top>thisUPage: </td><td>[" + thisUPage + "]</td></tr>"
document.write( strwrite );
</script><tr><td>
=================
获取IP
1、ASP.NET中获取
获取服务器的IP地址:
using System.Net;
string myIP,myMac;
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
if ( addressList.Length>1)
{
myIP = addressList[0].ToString();
myMac = addressList[1].ToString();
}
else
{
myIP = addressList[0].ToString();
myMac = "没有可用的连接";
}
myIP地址就是服务器端的ip地址。
获取客户端的ip地址,可以使用
//获取登录者ip地址
string ip = Request.ServerVariables["REMOTE_ADDR"].ToString();
2、通过JS获取
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
</head>
<body>
<object classid="CLSID:76A64158-CB41-11D1-8B02-00600806D9B6" id="locator" style="display:none;visibility:hidden"></object>
<object classid="CLSID:75718C9A-F029-11d1-A1AC-00C04FB6C223" id="foo" style="display:none;visibility:hidden"></object>
<form name="myForm">
<br/>MAC地址:<input type="text" name="macAddress">
<br/>IP地址:<input type="text" name="ipAddress">
<br/>主机名:<input type="text" name="hostName">
</form>
</body>
</html>
<script language="javascript">
var sMacAddr="";
var sIPAddr="";
var sDNSName="";
var service = locator.ConnectServer();
service.Security_.ImpersonationLevel=3;
service.InstancesOfAsync(foo, 'Win32_NetworkAdapterConfiguration');
</script>
<script FOR="foo" EVENT="OnObjectReady(objObject,objAsyncContext)" LANGUAGE="JScript">
if(objObject.IPEnabled != null && objObject.IPEnabled != "undefined" && objObject.IPEnabled == true){
if(objObject.IPEnabled && objObject.IPAddress(0) !=null && objObject.IPAddress(0) != "undefined")
sIPAddr = objObject.IPAddress(0);
if(objObject.MACAddress != null &&objObject.MACAddress != "undefined")
sMacAddr = objObject.MACAddress;
if(objObject.DNSHostName != null &&objObject.DNSHostName != "undefined")
sDNSName = objObject.DNSHostName;
}
</script>
<script FOR="foo" EVENT="OnCompleted(hResult,pErrorObject, pAsyncContext)" LANGUAGE="JScript">
myForm.macAddress.value=sMacAddr;
myForm.ipAddress.value=sIPAddr;
myForm.hostName.value=sDNSName;
</script>
温馨提示:内容为网友见解,仅供参考
第1个回答  推荐于2018-02-23
恩 你用的URL传的话是 xxxx.aspx?pname=xxxx 这样的吗?
那获取参数 就是Request.QueryString["pname"].ToString()来获取参数值
跳转页面 给值txtPName.Text = Request.QueryString["pname"].ToString();

这样回答你明白了么?本回答被提问者和网友采纳
第2个回答  2009-02-27
Page.Server.Transfer("xxxx.aspx?pname="+value);

在asp.net中怎么获得url"?"后面的参数?
Request.QueryString("yourname")这样的方式,说明该值是以GET方式发送,通过HTTP地址携带,比如我们常见的 http:\/\/zhidao.baidu.com\/question\/a.asp?yourname=123 要获取这个123,就用Request.QueryString("yourname")就可以了 想学ASP.NET直接去.csdn就可以.www.csdn.net 或者常去的.www.bc-cn...

在asp.net中怎么获得url"?"后面的参数?
Request.QueryString("yourname") 这样的方式,说明该值是以GET方式发送,通过HTTP地址携带,比如我们常见的 http:\/\/zhidao.baidu.com\/question\/a.asp?yourname=123 要获取这个123,就用Request.QueryString("yourname") 就可以了 想学ASP.NET直接去.csdn就可以.www.csdn.net 或者常去的.www.bc...

ASP.net怎么获取其他页面的URL
如果是从哪个页面跳转到当前页面,可以根据来源URL来取得。否则的话,可以通过在当前url后面加一个参数来取得:a.aspx?url=...

[Frombody]、[FromForm]傻傻分不清?ASP.NET Core获取请求参数方式总结...
从Body获取参数,通常通过表单提交或AJAX提交数据。使用Request.Body对象,它是一个Stream,保存了请求的body数据。要读取这个对象,必须先调用Request.EnableBuffering()方法。使用[FromBody]可以自动将body数据反序列化为模型,但注意请求的Content-Type必须是application\/json。使用[FromForm]则要求Content-Type...

asp.net页面如何获取传递给自己的参数
1、通过URL链接地址传递 (1) send.asp代码 protected void Button1_Click(object sender, EventArgs e){ Request.Redirect("Default2.aspx?username=honge");} (2) receive.aspx代码 string username = Request.QueryString["username"];\/\/这样可以得到参数值。2、POST方式传递 (1) send.asp代码 ...

asp。net,如果用户直接输入网址,如何获取网址对其做处理后在显示页面...
通过ASP.NET获取 如果测试的url地址是 http:\/\/www.test.com\/testweb\/default.aspx, 结果如下: Request.ApplicationPath: \/testweb Request.CurrentExecutionFilePath: \/testweb\/default.aspx Request.FilePath: \/testweb\/default.aspx Request.Path: \/testweb\/default.aspx Request.PhysicalApplicationPath...

ASP.NET C#如何获得网页URL
CovertToInt32(request.QueryString("nom"));上面漏了引号 "获得当前网页URL的最后8位字符"这里我们称为"查询字符串"

asp.net获取url的问题
这是编码问题,你在传递中文时先用UrlEncode把它给加密,然后接收时再UrlDecode解密即可。

asp.net 如何获取超链接路径的某段字符串
如果仅仅是获得? 后面的值可:string id=Request.QueryString["ID"];或者 string id=request["ID"];如果要获得本页或上一页的URl的话可以:Request.Url是当前url Request.RawUrl是上一个页面url 然后字符串截取即可

...进行转码后,如何在asp.net C# 代码中获取URL参数
直接HttpUtility.UrlDecode(Request["参数名称"].ToString())不就可以的到汉字了吗。

相似回答