【问题标题】:How to obtain the current url [duplicate]如何获取当前网址 [重复]
【发布时间】:2012-01-01 09:18:24
【问题描述】:

可能重复:
How to get the URL of the current page in C#

如果我在一个页面中说 http://myweb/folder/obtain.aspx?thevalue=3 ,我如何确定 url 在 c# 中是否包含 obtain.aspx?thevalue?。我只需要检查用户是否登陆了这个特定页面。

PS:我想我真的不需要检查?thevalue,而只需检查obtain.aspx

【问题讨论】:

  • @RichardD 那里的答案没有解释如何获取实际页面/文件的名称,它们只是显示了如何获取 URL。根据标题,它们是相同的,但问题的内容使它们不同。
  • 如何在 python 中做到这一点?

标签: c# asp.net


【解决方案1】:

试试这个:

//gets the current url
string currentUrl = Request.Url.AbsoluteUri;

//check the url to see if it contains your value
if (currentUrl.ToLower().Contains("obtain.aspx?thevalue"))
    //do something

【讨论】:

【解决方案2】:

这将为您提供准确的文件名 ( gain.aspx ) Request.Url.Segments[1]

【讨论】:

  • 在我的情况下,它是最后一段,即 3。我将如何修改上面的内容以获取 URL 的结尾(/ 之后的最后一部分)?
【解决方案3】:

Request.Url 应该包含您需要的所有内容。在你的情况下,你可以使用类似的东西

if( Request.Url.PathAndQuery.IndexOf("obtain.aspx") >= 0 )...

【讨论】:

    【解决方案4】:

    我建议使用Request.Url。要获取确切的文件名,您也可以尝试使用System.IO.Path

    var aspxFile = System.IO.Path.GetFileName(Request.Url.LocalPath);
    var landed = aspxFile.Equals("obtain.aspx", StringComparison.InvariantCultureIgnoreCase);
    if(landed) { // your code }
    

    【讨论】:

      【解决方案5】:

      Request.Url 将返回用户请求的确切 Uri。

      如果您想专门检查 thevalue,您最好在 Request.QueryString 中查找它

      【讨论】:

        【解决方案6】:

        它很丑,但你可以试试

        if (HttpContext.Current.HttpRequest.Url.AbsolutePath.Contains("/obtain.aspx"))
         // then do something
        

        【讨论】:

        • Contains 也会为“2obtain.aspx”提供一个 true...应该使用 Equals() & System.IO.Path.GetFileName(Request.Url.LocalPath);
        猜你喜欢
        • 1970-01-01
        • 2017-12-24
        • 2018-09-15
        • 1970-01-01
        • 2017-05-03
        • 2012-04-19
        相关资源
        最近更新 更多