ASP.net MVC to accept POST not UTF-8?

My app everywhere uses UTF-8 and change is not desirable. But: the old web gateway is sending me a POST with text in the format Windows1251. What is the easiest way to change the encoding of a single action of one controller on Windows1251?

public class Message<br>
{<br>
 public int id { get; set; }<br>
 public string phone { get; set; }<br>
 public string mes { get; set; }<br>
 public string to { get; set; }<br>
}<br>
<br / >
[HttpPost]<br>
public ActionResult Receive(Message msg)<br>
{<br>
AddIncomingMessage(msg);<br>
 return new EmptyResult();<br>
}<br>
October 8th 19 at 02:41
1 answer
October 8th 19 at 02:43
Solution
To edit the input data in its ModelBinder'e?
Do not fully understand.
If I understand correctly ModelBinder does what shifts the data from the Request.Form["mes"] in msg.mes. - roel_Romague commented on October 8th 19 at 02:46
Do not fully understand.
If I understand correctly ModelBinder does what shifts the data from the Request.Form["mes"] in msg.mes.
But in Request.Form encoding bezvozratno already beaten. - roel_Romague commented on October 8th 19 at 02:49
If you try right?

public class CustomModelBinder : DefaultModelBinder<br> {<br> public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)<br> {<br> Stream stream = controllerContext.HttpContext.Request.InputStream;<br> var reader = new StreamReader(stream, Encoding.GetEncoding(1251));<br> string text = reader.ReadToEnd();<br> <br / > var model = new SomeModel();<br> //here the model is populated from text<br> return model;<br> }<br> }

And exene:

public ActionResult SomeAction([ModelBinder(typeof(CustomModelBinder))]SomeModel m)<br> {<br> //...<br> } - ethyl.Kris commented on October 8th 19 at 02:52
Well, kraynyak so. Only in this case you need to manually populate the model from a text, and it asp.net mvc already knows, stupid to do his work :) - roel_Romague commented on October 8th 19 at 02:55
Just in this case it turns out not "stupid" and "smart" — indicating the encoding) + if parse text as NameValueCollection coll = HttpUtility.ParseQueryString(text), the implementation becomes quite trivial. - ethyl.Kris commented on October 8th 19 at 02:58
Well, in General, will do so - roel_Romague commented on October 8th 19 at 03:01

Find more questions by tags ASP.NET