用特殊字符编码文本

时间:2014-06-05 17:35:26

标签: c# url encoding

我有一个'%'的文字,我想在链接中使用,但我收到错误,(错误的请求)

样品:

@{

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>

我使用这条路线:

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

1 个答案:

答案 0 :(得分:1)

HtmlEncode用于将特殊字符转换为 HTML 。使用UrlEncode将特殊字符编码为有效的网址

var url = HttpUtility.UrlEncode(title);