简体   繁体   中英

Error 400 (Bad Request) when get a file which name contains & character in C# ASP.NET MVC3

I have an issue regarding the filename which contains & character.

I have to get an image which name is: Test&TestAgain.jpg . In ASP.NET MVC3 application, in View side, I put

Url.Content( "~/Content/Images/"+ filename );

In Chrome, I see error 400 Bad Request because of filename which contains & .

I think that & is used for query string and browser interprets file name as query string.

Because filename doesn't contain ? then browser throw that error. ( A potentially dangerous Request.Path value was detected from the client (&). )

It is a way (workaround) to fix this without replace that character ?

You need to UrlEncode the path:

Url.Content("~/Content/Images/" + HttpServerUtility.UrlEncode(filename));

Edit:

Seems it doesn't work. The only way I can think of right now is to allow resticted characters and enable VerificationCompatibility by setting in the registry:

// 32-bit IIS
HKLM\SOFTWARE\Microsoft\ASP.NET VerificationCompatibility = 1

// 64-bit IIS
HKLM\SOFTWARE\Wow6432Node\Microsoft\ASP.NET VerificationCompatibility = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM