Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.NET
<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>
Posted
Updated 8-Dec-15 3:06am
v2
Comments
[no name] 8-Dec-15 9:19am    
hello sir anybody help me

1 solution

Cleaning up the string is relatively straightforward. Something like this should work:
C#
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.NET
<asp:HyperLink ID="lbstatepincode" runat="server"
    NavigateUrl='<%# "~/" + LinkHelper.UrlSlug(Eval("state_name_without_pin", "{0}")) %>'  
    Text='<%#Bind("state_name")%>'
/>

How you then map that "cleaned-up" URL back to the correct state is more complicated, and depends on your URL routing strategy.
 
Share this answer
 
v3
Comments
[no name] 8-Dec-15 14:38pm    
hello sir this code is work good

but i have one query, i want to replace & to and

how i do to replace & to and please solve it.
thanks a lots sir
Richard Deeming 8-Dec-15 14:39pm    
The site seems to have swallowed part of your comment.

Remember, you need to HTML-encode the comment - in particular, replace & with &amp;.


Never mind - I've just realised that you want to replace "&" with "and". :)
Richard Deeming 8-Dec-15 15:15pm    
I've updated the answer - you need to remove '&' from the IllegalChars collection, and add an extra line to the UrlSlug method:

slug = slug.Replace("&", "-and-");
[no name] 8-Dec-15 15:19pm    
thanks a lot sir working awesome
[no name] 8-Dec-15 15:15pm    
please sir tell me how to do this in c#
to replace & to and in navigateurl

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900