Sometimes it’s needed to set different request/response encoding for some of the requests. For example when some third party sends requests (or responses) in some predefined encoding and doesn’t support UTF8, whereas ASP.NET uses UTF8 for all requests and responses by default.

ASP.NET allows us to change that encoding through web.config’s <globalization /> element, like:

<globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" />

But this way it affects all the request and that’s not what I want. Luckily, ASP.NET allows web.config file inheritance, by creating separate web.config file in a subfolder or using <location path=””> element in main web.config file.

In case of webforms it’s simple – we need to create location-based configuration for one of our .aspx files. But how to achieve it with MVC you might ask?

Actually, it’s exactly the same. For path part you need to provide full route url to you action. Path attribute doesn’t need to be a physical file or folder, but path in the sense of url. Only drawback is that when changing routes you must remember to change it in the web.config also. And here is the final result, which affects only one action:

<configuration>
...
<location path="path/to/your/actionmethod">
	<system.web>
		<globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" />
	</system.web>
</location>
...
</configuration>
  • Twitter
  • Facebook
  • Technorati Favorites
  • Share/Bookmark