【问题标题】:Getting URL of current page获取当前页面的 URL
【发布时间】:2014-05-14 12:42:27
【问题描述】:

我需要检查页面的当前 URL 是否包含特定字符串。现在,我知道你可以通过ClientQueryString.Contains 做到这一点。但是,我要修改的页面已经继承了System.Web.UI.UserControl 而且我知道要使用ClientQueryString.Contains,您必须继承System.Web.UI.Page

由于我不能继承其他东西,所以我可以使用这个或其他方法吗?

【问题讨论】:

标签: asp.net vb.net


【解决方案1】:

Request.Urlat this site有全套例子 要隔离查询字符串,您可以执行以下操作: Request.Url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped) 或者 Request.QueryString 这是我对您尝试从自己的代码中执行的操作的假设。

【讨论】:

    【解决方案2】:

    请检查以下内容以获取URL的不同部分

    示例(示例网址)

    http://localhost:60527/MyWeb/Default2.aspx?QueryString1=1&QuerrString2=2

    代码

    Response.Write("<br/> " + HttpContext.Current.Request.Url.Host);
    Response.Write("<br/> " + HttpContext.Current.Request.Url.Authority);
    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
    /MyWeb/Default2.aspx
    /MyWeb
    http://localhost:60527/MyWeb/Default2.aspx?QueryString1=1&QuerrString2=2
    /MyWeb/Default2.aspx?QueryString1=1&QuerrString2=2 
    

    您可以复制粘贴上面的示例代码并在具有不同 URL 的 asp.net 网络表单应用程序中运行它。

    See here for more Details

    【讨论】:

      【解决方案3】:

      你能用 HttpRequest.Url 吗?

      Request.URL documentation

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-17
        • 1970-01-01
        • 2013-03-01
        • 2021-07-23
        • 2010-11-26
        • 2010-11-28
        • 2016-03-01
        • 2015-09-30
        相关资源
        最近更新 更多