谁能帮助我在C#中获得ASP.NET当前工作页面的URL?
试试这个:
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
你可能有时需要从URL中获取不同的值。
下面的例子显示了提取URL不同部分的不同方法。
例子: (样本网址)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2。
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
CODE
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);
输出
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
你可以复制粘贴以上示例代码&。 用不同的URL在asp.net web form应用程序中运行它。
我也推荐阅读ASP.Net Routing,如果你可能使用ASP Routing,那么你不需要使用传统的URL和查询字符串。
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
只是分享一下,因为这是我的解决方案,感谢Canavar'的帖子。
如果你有这样的事情。
"http://localhost:1234/Default.aspx?un=asdf&somethingelse=fdsa"
或像这样。
"https://www.something.com/index.html?a=123&b=4567"
而你只想要用户会输入的部分,那么这个就可以了。
String strPathAndQuery = HttpContext.Current.Request.Url.PathAndQuery; String strUrl = HttpContext.Current.Request.Url.AbsoluteUri.Replace(strPathAndQuery, "/");
这将导致这些。
"http://localhost:1234/" "https://www.something.com/"
如果你只想要http:// 和第一个斜线之间的部分
string url = Request.Url.Host;
如果从这个页面调用,将返回 stackoverflow.com
这里是完整的分类
request.rawurl将给出当前页面的内容。 它给出了您所需要的准确路径
使用HttpContext.Current.Request.RawUrl。
HttpContext.Current.Request.RawUrl
如果你想得到
localhost:2806
从
http://localhost:2806/Pages/
然后使用。
HttpContext.Current.Request.Url.Authority
给需要在global.asax文件中输入路径/url的人一个提示。
如果你需要在global.asax > Application_Start中运行,并且你的应用池模式是integrated,那么你将收到以下错误。
Request is not available in this context exception in >.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);
这意味着你可以使用相同的方法,例如
string url = uri.AbsoluteUri; // http://localhost:1302/TESTERS/Default6.aspx string host = uri.host // localhost
Regex方式。
https://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex
我想它只要返回绝对路径就够了。
Path.GetFileName( Request.Url.AbsolutePath )
使用System.IO.Sql.Implement.Ltd;
试试这个:
你可能有时需要从URL中获取不同的值。
下面的例子显示了提取URL不同部分的不同方法。
例子: (样本网址)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
。CODE
输出
你可以复制粘贴以上示例代码&。 用不同的URL在asp.net web form应用程序中运行它。
我也推荐阅读ASP.Net Routing,如果你可能使用ASP Routing,那么你不需要使用传统的URL和查询字符串。
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
只是分享一下,因为这是我的解决方案,感谢Canavar'的帖子。
如果你有这样的事情。
或像这样。
而你只想要用户会输入的部分,那么这个就可以了。
这将导致这些。
如果你只想要http:// 和第一个斜线之间的部分
如果从这个页面调用,将返回 stackoverflow.com
这里是完整的分类
request.rawurl将给出当前页面的内容。 它给出了您所需要的准确路径
使用
HttpContext.Current.Request.RawUrl
。如果你想得到
从
然后使用。
给需要在global.asax文件中输入路径/url的人一个提示。
如果你需要在global.asax > Application_Start中运行,并且你的应用池模式是integrated,那么你将收到以下错误。
在这种情况下,你需要使用这个。
希望能帮助别人...
通过搜索,我看到了这个页面,但它并不是我所要找的。在这里发帖,是为了防止有人也在寻找我所寻找的东西,在这个页面上。
如果你只有一个字符串值,有两种方法可以做到这一点。
.NET方式。
和@Canavar一样,但你可以实例化一个新的Uri对象
这意味着你可以使用相同的方法,例如
Regex方式。
https://stackoverflow.com/questions/27745/getting-parts-of-a-url-regex
我想它只要返回绝对路径就够了。
使用System.IO.Sql.Implement.Ltd;