谁能帮助我在获得ASP当前工作页面的URL。NET在c# ?
当前回答
只是分享这是我的解决方案,感谢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/"
其他回答
我想返回绝对路径就足够了。
Path.GetFileName( Request.Url.AbsolutePath )
使用先;
有时可能需要从URL获取不同的值。
下面的例子展示了提取URL不同部分的不同方法
示例:(示例URL)
http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
CODE
Response.Write("<br/>Host " + HttpContext.Current.Request.Url.Host);
Response.Write("<br/>Authority: " + HttpContext.Current.Request.Url.Authority);
Response.Write("<br/>Port: " + HttpContext.Current.Request.Url.Port);
Response.Write("<br/>AbsolutePath: " + HttpContext.Current.Request.Url.AbsolutePath);
Response.Write("<br/>ApplicationPath: " + HttpContext.Current.Request.ApplicationPath);
Response.Write("<br/>AbsoluteUri: " + HttpContext.Current.Request.Url.AbsoluteUri);
Response.Write("<br/>PathAndQuery: " + HttpContext.Current.Request.Url.PathAndQuery);
输出
Host: localhost
Authority: localhost:60527
Port: 60527
AbsolutePath: /WebSite1test/Default2.aspx
ApplicationPath: /WebSite1test
AbsoluteUri: http://localhost:60527/WebSite1test/Default2.aspx?QueryString1=1&QueryString1=2
PathAndQuery: /WebSite1test/Default2.aspx?QueryString1=1&QueryString2=2
你可以复制粘贴上面的示例代码,并在asp.net web表单应用程序中使用不同的URL运行它。
我也推荐阅读ASP。如果你可能使用ASP路由,那么你不需要使用传统的URL与查询字符串。
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
我搜索了一下这个页面,但这并不是我想要的。在这里发布,以防其他人也在这个页面上寻找我。
如果你只有一个字符串值,有两种方法。
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
正则表达式:
获取URL的部分(正则表达式)
请求。Rawurl会给出当前页面的内容 它给出了您需要的确切路径
使用HttpContext.Current.Request.RawUrl
如果你想要
localhost:2806
从
http://localhost:2806/Pages/
然后使用:
HttpContext.Current.Request.Url.Authority
推荐文章
- ASP是什么?NET Identity的IUserSecurityStampStore<TUser>接口?
- net HttpClient。如何POST字符串值?
- 我如何使一个方法的返回类型泛型?
- 何时处理CancellationTokenSource?
- ASP。网网络。config: configSource vs.文件属性
- 如何获取正在执行的程序集版本?
- AutoMapper vs valueinjector
- 为什么控制台不。Writeline,控制台。在Visual Studio Express中编写工作?
- 什么是.NET程序集?
- 字符串不能识别为有效的日期时间“格式dd/MM/yyyy”
- 函数应该返回空对象还是空对象?
- 如何转换日期时间?将日期时间
- 如何在c#中连接列表?
- 在c#中引用类型变量的“ref”的用途是什么?
- 防止在ASP中缓存。NET MVC中使用属性的特定操作