Is MVC2 ASP.Net URLDecoding automatically?

我的未来我决定 提交于 2019-12-10 10:50:54

问题


So I was calling the Controller in MVC2 like this and I accessed the classic querystrings just fine. Please notice that the second parameter thing2 is URLEncoded already and again retrieving the URLEncoded querystring and URLDecoding is no problem. My example looks like this...

http://mydomain.com/controller?thing1=1544&thing2=somethingURLEncoded

Now I try to move to the MVC2 ASP.Net way of handling parameters and make myself a nice custom MapRoute. I test it to see that it works with a simple test...

http://mydomain.com/controller/Index/1544/999

I debug with VS2010 step inside of my Index method inside of my controller with success!

I then decide to take the next step and change the last parameter on the URL to be a URLEncoded value...

http://mydomain.com/controller/Index/1544/somethingURLEncoded

Problem I see after executing this in my browser is that it almost looks like MVC2 ASP.Net is automagically URLDecoding this before I get to step inside of my Index method inside of my controller.

What gives? I thought I would be able to get inside of my controller first and the secondly do a URLDecode of my own. Because the orginal data was AES encrypted and had forward slashes in it.., have my parameter prematurely URLDecoding is not a side effect I can plan around.

Please help.


回答1:


Yes, MVC automatically url decodes action parameters. But you can still access the url encoded version through query string.




回答2:


So what I did was to Base64 encode instead of URLEncode my AES encrypted data in my Action Parameter.




回答3:


As ASP.NET MVC is based on ASP.NET engine (not surprisingly), properly encoded URLs are automatically properly URL decoded by the engine so you don't ever need to ask questions. Simply:

public ActionResult Index(string q)
{
    // TODO : Use the q parameter here as is without ever caring of URL decoding it.
    // It's the caller of this action responsibility to properly URL encode his data
    return View();
}


来源:https://stackoverflow.com/questions/3367216/is-mvc2-asp-net-urldecoding-automatically

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