Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较页面';s指向a';s href_C#_Html_Asp.net - Fatal编程技术网

C# 比较页面';s指向a';s href

C# 比较页面';s指向a';s href,c#,html,asp.net,C#,Html,Asp.net,当页面的url等于的href时,我会尝试这样做,但它会改变某些内容的类 它确实会在点击链接时更改页面,但不会更改 以下是我所做的: html: 这门课很有效,我已经检查过了。 顺便说一下,此代码位于母版页上,母版页连接到中的两个页面 Tnx帮助:D您需要将相对url转换为绝对url,以便与页面url进行比较 出于某种奇怪的原因,我在Url中找不到任何支持父路径的Url帮助程序,因此我唯一能想到的解决方法是使用Server.MapPath()来实现以下目的: string pagePath = S

当页面的url等于
的href时,我会尝试这样做,但它会改变某些内容的类

它确实会在点击链接时更改页面,但不会更改
  • 以下是我所做的:

    html:

    这门课很有效,我已经检查过了。 顺便说一下,此代码位于母版页上,母版页连接到
    中的两个页面


    Tnx帮助:D

    您需要将相对url转换为绝对url,以便与页面url进行比较

    出于某种奇怪的原因,我在Url中找不到任何支持父路径的Url帮助程序,因此我唯一能想到的解决方法是使用
    Server.MapPath()
    来实现以下目的:

    string pagePath = Server.MapPath(Request.FilePath);
    Dictionary<HtmlAnchor, Label> anchorMapping = new Dictionary<HtmlAnchor, Label>();
    anchorMapping.Add(A1, L1);
    anchorMapping.Add(A2, L2);
    foreach (HtmlAnchor currentAnchor in anchorMapping.Keys)
    {
        if (Server.MapPath(currentAnchor.HRef).Equals(pagePath))
        {
            anchorMapping[currentAnchor].Attributes.Add("class", "active");
            break;
        }
    }
    
    string pagePath=Server.MapPath(Request.FilePath);
    Dictionary anchorMapping=新字典();
    加上(A1,L1);
    添加(A2,L2);
    foreach(锚定映射中的HtmlAnchor currentAnchor.Keys)
    {
    if(Server.MapPath(currentAnchor.HRef).Equals(pagePath))
    {
    anchorMapping[currentAnchor].Attributes.Add(“类”,“活动”);
    打破
    }
    }
    
    问题是A1。HRef返回相对url。另一方面,Request.Url返回absoluteUrl

    为了修复它,您需要对“超链接”使用服务器控制,并将其解析回绝对路径

    <ul>
        <li id="L1" runat="server">
            <asp:HyperLink runat="server" ID="A1HyperLink" 
                NavigateUrl="~/newsFeed/allEr.aspx">
                <span>Er</span>
            </asp:HyperLink>
        </li>
    </ul>
    
    string url = Request.Url.PathAndQuery;
    string a1 = ResolveUrl(A1HyperLink.NavigateUrl);
    if (string.Equals(a1, url, StringComparison.InvariantCulture))
    {
        L1.Attributes.Add("class", "active");
    }
    
    字符串url=Request.url.PathAndQuery; 字符串a1=ResolveUrl(a1.HRef); if(string.Equals(a1,url,StringComparison.InvariantCulture)) { L1.属性。添加(“类”、“活动”); }
    我怀疑比较考虑的是完整的URL,而且
    http://www.yoursite.com/newsFeed/allEr.aspx
    。/newsFeed/allEr.aspx
    将永远匹配。那么替代方案是什么呢?还要小心。。。url可能包含url查询字符串参数。看看.Net Framework中的
    Uri
    类。有很多有用的方法。
    string pagePath = Server.MapPath(Request.FilePath);
    Dictionary<HtmlAnchor, Label> anchorMapping = new Dictionary<HtmlAnchor, Label>();
    anchorMapping.Add(A1, L1);
    anchorMapping.Add(A2, L2);
    foreach (HtmlAnchor currentAnchor in anchorMapping.Keys)
    {
        if (Server.MapPath(currentAnchor.HRef).Equals(pagePath))
        {
            anchorMapping[currentAnchor].Attributes.Add("class", "active");
            break;
        }
    }
    
    <ul>
        <li id="L1" runat="server">
            <asp:HyperLink runat="server" ID="A1HyperLink" 
                NavigateUrl="~/newsFeed/allEr.aspx">
                <span>Er</span>
            </asp:HyperLink>
        </li>
    </ul>
    
    string url = Request.Url.PathAndQuery;
    string a1 = ResolveUrl(A1HyperLink.NavigateUrl);
    if (string.Equals(a1, url, StringComparison.InvariantCulture))
    {
        L1.Attributes.Add("class", "active");
    }
    
    <div id='settingNev' >
        <ul >
            <li id="L1"  runat="server"><a id="A1" 
               href="../newsFeed/allEr.aspx" 
               runat="server"><span>Er</span></a></li>
        </ul>
    </div>
    
    string url = Request.Url.PathAndQuery;
    string a1 = ResolveUrl(A1.HRef);
    if (string.Equals(a1, url, StringComparison.InvariantCulture))
    {
        L1.Attributes.Add("class", "active");
    }