How can I modify or handle URL special characters in MVC?

点点圈 提交于 2021-02-20 05:13:07

问题


I have an MVC web application. The URL for a particular area is coming in as:

http://localhost/General/Bpa%3fapplication%3dTrf%23/GeneralInputs

This causes a "The resource cannot be found." error. However, if I change the URL to

http://localhost/General/Bpa?application=Trf#/GeneralInputs

then everything works. I can see from using some route debugging tricks that the controller in the first case is: "Bpa?application=Trf#", whereas the second one is: "Bpa", which is correct. How can I account for this or substitute for the encoded characters?


回答1:


You're going to want to use this on your url:

string fixedUrl = System.Uri.UnescapeDataString(yourUrlHere); 

Hope that works out for you!




回答2:


The encoding of the first URL is wrong. If you look at RFC 3986 you will find in 2.4 the paragraph

When a URI is dereferenced, the components and subcomponents
significant to the scheme-specific dereferencing process (if any)
must be parsed and separated before the percent-encoded octets within those components can be safely decoded, as otherwise the data may be
mistaken for component delimiters.

That means the URL is decomposed by unencoded characters (in this case the ? matters). If the encoded string #3f is used, then the framework would have to look for a controller named "Bpa?application=Trf#" and not "Bpa". Thus a 404 / resource not found is returned.

You should not fix it on the server side; you will have to change the place where the wrong url http://localhost/General/Bpa%3fapplication%3dTrf%23/GeneralInputs is generated.



来源:https://stackoverflow.com/questions/45313208/how-can-i-modify-or-handle-url-special-characters-in-mvc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!