How do I post data to MVC Controller using HttpWebRequest?

后端 未结 3 1206
离开以前
离开以前 2021-01-05 16:03

I am trying to post data to MVC controller action but have been unsuccessful so far.

Here is the structure of the post data:

private string makeHttp         


        
相关标签:
3条回答
  • 2021-01-05 16:21

    Too complicated and unsafe your client code with all those requests and responses. You are not encoding any of your request parameters, not to mention this XML which is probably gonna break everything if you don't encode it properly.

    For this reason I would simplify and leave the plumbing code about encoding, etc... to the .NET framework:

    using (var client = new WebClient())
    {
        var values = new NameValueCollection
        {
            { "uid", hwid },
            { "localization", localization },
            { "label", label },
            { "interchangeDocument", interchangeFile.OuterXml },
        };
        var result = client.UploadValues(controllerUrl, values);
        // TODO: do something with the results returned by the controller action
    }
    

    As far as the server side is concerned, as every properly architected ASP.NET MVC application, it would obviously use a view model:

    public class MyViewModel
    {
        public string Uid { get; set; }
        public string Localization { get; set; }
        public string Label { get; set; }
        public string InterchangeDocument { get; set; }
    }
    

    with:

    [HttpPost]
    public ActionResult Foo(MyViewModel model)
    {
        // TODO: do something with the values here
        ...
    }
    

    Obviously this could be taken a step further by writing a view model reflecting the structure of your XML document:

    public class Foo
    {
        public string Bar { get; set; }
        public string Baz { get; set; }
    }
    

    and then your view model will become:

    public class MyViewModel
    {
        public string Uid { get; set; }
        public string Localization { get; set; }
        public string Label { get; set; }
        public Foo InterchangeDocument { get; set; }
    }
    

    and the last part would be to write a custom model binder for the Foo type that will use a XML serializer (or whatever) to deserialize back the InterchangeDocument POSTed value into a Foo instance. Now that's serious business.

    0 讨论(0)
  • 2021-01-05 16:22

    I just found that you can call a controller, even a dependency injected one, even from a Web Forms code behind using the "T4MVC" Nuget package:

    https://github.com/T4MVC/T4MVC

    0 讨论(0)
  • 2021-01-05 16:39

    I'm wrestling the same beast over here: Trying to set up a controller action as Xml endpoint

    You may be getting the internal server error because either you have page validation on (solution: addotate with ValidateInput(false)), or you're not sending an Accept-Encoding header with your request. I would very much like to hear how I can get MVC to accept posted input without the Accept-Encoding HTTP header...

    0 讨论(0)
提交回复
热议问题