Rodrigo Porcionato
Rodrigo Porcionato

Reputation: 155

enconde text with special characters

I have one text that have '%' and I would like to use in link, but I receice a error,(bad request)

Sample:

@{

var title = "less than 1% ."; ///this I would like to pass in url

var url = HttpUtility.HtmlEncode(title); //I try it, but not work!

}

<a href="/cidades/cidade/Categoria/@url">go to city</a>

I use this route:

routes.MapRoute(
            name: "Categoria",
            url: "{controller}/{action}/categoria/{nome}",
            defaults: new { controller = "Cidades", action = "Index", id = UrlParameter.Optional }
        );

Upvotes: 0

Views: 69

Answers (1)

D Stanley
D Stanley

Reputation: 152521

HtmlEncode is for translating special charaters to HTML. Use UrlEncode to encode special characters into a valid URL:

var url = HttpUtility.UrlEncode(title); 

Upvotes: 1

Related Questions