谁能帮助我在获得ASP当前工作页面的URL。NET在c# ?
当前回答
如果你想要
localhost:2806
从
http://localhost:2806/Pages/
然后使用:
HttpContext.Current.Request.Url.Authority
其他回答
有时可能需要从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
试试这个:
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
只是分享这是我的解决方案,感谢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/"
给那些需要全局路径/url的人一个提示。asax文件;
如果你需要全局运行。asax > Application_Start和你的应用池模式是集成的,然后你会收到以下错误:
请求在此上下文异常中不可用 Application_Start。
在这种情况下,你需要使用这个:
System.Web.HttpRuntime.AppDomainAppVirtualPath
希望能帮助别人。
如果你想要
localhost:2806
从
http://localhost:2806/Pages/
然后使用:
HttpContext.Current.Request.Url.Authority
推荐文章
- 防止在ASP中缓存。NET MVC中使用属性的特定操作
- 转换为值类型'Int32'失败,因为物化值为空
- c#中有任何连接字符串解析器吗?
- 在Linq中转换int到字符串到实体的问题
- 是否可以动态编译和执行c#代码片段?
- 创建自定义MSBuild任务时,如何从c#代码获取当前项目目录?
- <system. >之间的差异。web>和<system.webServer>?
- 为什么ASP。NET web表单需要Runat="Server"属性吗?
- c#和Java的主要区别是什么?
- 在c#中创建一个特定时区的DateTime
- .NET中的属性是什么?
- csproj文件中的“Service Include”是干什么用的?
- 如何使用try catch进行异常处理是最佳实践
- 替换字符串中第一次出现的模式
- .NET中字节的字面后缀?