如何在asp.net中替换转发器中超链接的导航url的值 [英] how to replace value of navigate url of hyperlink in repeater in asp.net

查看:95
本文介绍了如何在asp.net中替换转发器中超链接的导航url的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好先生我正在使用网址重写



我的数据库有像&,space,double space这样的文字



i想要在转发器的超链接中替换bind navigation url的值



因为即将发生的错误



从客户端检测到潜在危险的Request.Path值(&)。





代码是





hello sir i am using url re writing

my database have text like &, space, double space

i want to replace value of bind navigate url in hyperlink in repeater

because coming error

A potentially dangerous Request.Path value was detected from the client (&).


code is


<asp:Repeater ID="rptstatelist" runat="server" 

            onitemcommand="rptstatelist_ItemCommand">
<itemtemplate>
<ul style="margin:0px; padding:0px;">
   <li style="font-size:15px; list-style-type:disc; color:#585F5F; font-family: sans-serif;"> <asp:HyperLink ID="lbstatepincode" NavigateUrl='<%#"~/" +Eval("state_name_without_pin")%>'  Text='<%#Bind("state_name")%>' runat="server"></li>
   </ul>
   </itemtemplate>

推荐答案

清理字符串相对简单。这样的事情应该有效:

Cleaning up the string is relatively straightforward. Something like this should work:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

public static class LinkHelper
{
    private static readonly Regex ExtraHyphen = new Regex("\\-{2,}", RegexOptions.Compiled);
    
    private static readonly HashSet<char> IllegalChars = new HashSet<char>
    {
        ':', '/', '?', '#', '[', ']', '@', '*', '.', ',', '"', '\'',
    };
    
    private static string RemoveExtraHyphen(string text)
    {
        if (string.IsNullOrWhiteSpace(text)) return string.Empty;
        return ExtraHyphen.Replace(text, "-");
    }
    
    private static string RemoveDiacritics(string text)
    {
        if (string.IsNullOrWhiteSpace(text)) return string.Empty;
        
        string normalized = text.Normalize(NormalizationForm.FormD);
        
        return new string(normalized
            .Where(c => UnicodeCategory.NonSpacingMark != CharUnicodeInfo.GetUnicodeCategory(c))
            .ToArray());
    }
    
    public static string UrlSlug(string text)
    {
        if (string.IsNullOrWhiteSpace(text)) return string.Empty;
        
        string slug = new string(text
            .Where(c => !IllegalChars.Contains(c))
            .Select(c => ' ' == c ? '-' : c)
            .ToArray());
        
        slug = slug.Replace("&", "-and-");
        
        slug = RemoveDiacritics(slug);
        slug = RemoveExtraHyphen(slug);
        return HttpUtility.UrlEncode(slug).Replace("%", string.Empty);
    }
}




<asp:HyperLink ID="lbstatepincode" runat="server"

    NavigateUrl='<%# "~/" + LinkHelper.UrlSlug(Eval("state_name_without_pin", "{0}")) %>'  

    Text='<%#Bind("state_name")%>'

/>



然后如何将已清理的网址映射回正确的状态更复杂,取决于您的URL路由策略。


How you then map that "cleaned-up" URL back to the correct state is more complicated, and depends on your URL routing strategy.


这篇关于如何在asp.net中替换转发器中超链接的导航url的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆