Can*_*var 881
试试这个 :
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
Run Code Online (Sandbox Code Playgroud)
Lea*_*ing 472
您有时可能需要从URL获取不同的值.
下面的示例显示了提取URL的不同部分的不同方法
示例:(示例网址)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
码
Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/> " + HttpContext.Current.Request.Url.Port);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/> " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/> " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/> " + HttpContext.Current.Request.Url.PathAndQuery);
Run Code Online (Sandbox Code Playgroud)
OUTPUT
localhost
localhost:60527
60527
/WebSite1test/Default2.aspx
/WebSite1test
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
Run Code Online (Sandbox Code Playgroud)
您可以复制上面示例代码的粘贴,并在具有不同URL的asp.net Web表单应用程序中运行它.
我还建议您阅读ASP.Net路由,以防您使用ASP路由,然后您不需要使用带有查询字符串的传统URL.
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
Soe*_*hay 105
因为这是我的解决方案,感谢Canavar的帖子.
如果您有这样的事情:
"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
Run Code Online (Sandbox Code Playgroud)
或者像这样:
"https://www.something.com/index.html?a=123&b=4567"
Run Code Online (Sandbox Code Playgroud)
并且您只想要用户输入的部分然后这将起作用:
String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
Run Code Online (Sandbox Code Playgroud)
这将导致这些:
"http://localhost:1234/"
"https://www.something.com/"
Run Code Online (Sandbox Code Playgroud)
rom*_*n m 48
如果你只想要http://和第一个斜杠之间的部分
string url = Request.Url.Host;
Run Code Online (Sandbox Code Playgroud)
如果从此页面调用,将返回stackoverflow.com
这是完整的细分
Tho*_*mas 14
如果你想得到
localhost:2806
Run Code Online (Sandbox Code Playgroud)
从
http://localhost:2806/Pages/
Run Code Online (Sandbox Code Playgroud)
然后使用:
HttpContext.Current.Request.Url.Authority
Run Code Online (Sandbox Code Playgroud)
dvd*_*dmn 12
需要global.asax文件中路径/ url的人的提示;
如果您需要在global.asax> Application_Start中运行它并且您的应用程序池模式已集成,那么您将收到以下错误:
Application_Start中的此上下文异常中不提供请求.
在这种情况下,您需要使用此:
System.Web.HttpRuntime.AppDomainAppVirtualPath
希望能帮助别人..
搜索让我看到了这个页面,但这并不是我想要的.发布在这里以防其他人在这个页面上寻找我的土地.
如果您只有字符串值,有两种方法可以做到这一点.
.NET方式:
与@Canavar相同,但您可以实例化一个新的Uri对象
String URL = "http://localhost:1302/TESTERS/Default6.aspx";
System.Uri uri = new System.Uri(URL);
Run Code Online (Sandbox Code Playgroud)
这意味着您可以使用相同的方法,例如
string url = uri.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string host = uri.host
// localhost
Run Code Online (Sandbox Code Playgroud)
正则表达方式:
我想它足以返回绝对路径..
Path.GetFileName( Request.Url.AbsolutePath )
Run Code Online (Sandbox Code Playgroud)
使用System.IO;
归档时间: |
|
查看次数: |
1034649 次 |
最近记录: |