I am developing an ASP.NET MVC Website. In my website, I am doing url rewriting in RouteConfig for better SEO. But I am having a problem with that. The problem is with Encoding URL route data value.
I rewrite a URL in RouteConfig like this:
routes.MapRoute(
"",
"places/category/{categoryName}/{category}",
new { controller = "Item", action = "List", categoryName = "", category = 0 },
new string[] { "AyarDirectory.Web.Controllers" }
);
In my HTML, I created an anchor link like this:
<a href="@Url.Content("~/places/category/" + HttpUtility.UrlEncode(category.Name) + "/" + category.Id)">
Content
</a>
When I click on it, if the name does not contain characters like "+","%" after encoded, it is always showing 404. It the value does not contains space and /(so no characters like % and +) after encoded, it is working fine.
I catch value in action like:
HttpUtility.UrlDecode(value)
Why it is not working? How can I encode route data value? Even if the value contains space, if I don't encode value, it is working fine. The result URL will be something like this with space.
http://localhost:50489/places/guest house/4
So if I use without encoding value, it the value contains "/", it will be error. Why is that happening and how can I encode URL route data value?