ASP.NET MVC Framework encoding of special characters?

0 like 0 dislike
Good evening Habr!

Recently started to learn ASP.NET MVC Framework and was faced with the following problem. Let's say I want to make a controller to search for a website of this type /search/<word>. But the user can type in the search anything, including /, and * and other special characters .NET is my feeling first translates the characters from the "encoding %" in the "normal view" and then determines what controller to pass.

Let the user enter a search in the "movies/games", the website will take a request like /search/%E8%E3%F0%FB%2F%F4%E8%EB%FC%EC%FB and that's the %2F will be first translated into " / " and the query in /search/movies/games and then will be determined by the controller. Accordingly, in my route defined as /search/{query}/{page} where page is restricted is either absent or consists of numbers, the query will not fall and the website will return a 404 error.

How to escape special characters?
by | 8 views

1 Answer

0 like 0 dislike
Add an asterisk to the rule:
\rroutes.MapRoute("Search", "Search/{*str}",
new { controller = "Search" action = "Index", str = UrlParameter.Optional });

And will not be 404

Related questions

110,608 questions
257,186 answers
32,869 users