Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receive POST data in non-UTF-8 encoding (ASP.NET MVC)

my ASP.NET MVC Application is UTF-8, but I receive POST request in Encoding.Default from third-party app out of my control.

What is sanest and simplest way to change request encoding for only one action of one controller? (Rest of my application should remain UTF-8).

public class Message
{
 public int id { get; set; }
 public string phone { get; set; }
 public string mes { get; set; }
 public string to { get; set; }
}

[HttpPost]
public ActionResult Receive(Message msg)
{
        AddIncomingMessage(msg);
        return new EmptyResult();
}
like image 443
Leotsarev Avatar asked Feb 28 '12 13:02

Leotsarev


1 Answers

You can do Action based request encoding in ASP.NET MVC with web.config location tag setting.

Sample web.config:

<configuration>
...
<location path="path/to/your/actionmethod">
    <system.web>
        <globalization requestEncoding="ISO-8859-1" responseEncoding="ISO-8859-1" />
    </system.web>
</location>
...
</configuration>

http://www.siimviikman.com/2012/06/12/action-based-request-encoding-in-asp-net-mvc/

like image 112
ishakkulekci Avatar answered Oct 15 '22 20:10

ishakkulekci