2

This does not seem to be as easy as I thought. I found some solutions on the web, but they are not working for me. I have an ASP.Net MVC 3 project with the Microsoft ASP.Net Web API 2.1 nuget package installed. Now, I want to be able to read data posted to a web api controller. The data sent will vary, so I cannot used a strongly typed ViewModel.

Here are the solutions I tried:

    public void Post([FromBody]string value)
    {
        ...
    }


    public void Post([FromBody]List<string> values)
    {
        ...
    }


    public void Post([FromBody]NameValueCollection values)
    {
        ...
    }

But my value or values variables are always empty. I know the controller is receiving data however because I can check it by accessing (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"].Request.Form. It does not look like the proper way to retrieve the data though. There ought to be a cleaner way.

UPDATE:

Here is how I am posting the information:

I am posting the data from another controller in the same web application:

    public ActionResult SendEmailUsingService()
    {
        dynamic email = new ExpandoObject();

        email.ViewName = "EmailTest";
        email.From = "[email protected]";
        email.To = "[email protected]";
        email.Fullname = "John Smith";
        email.Url = "www.mysite.com";

        IDictionary<string, object> data = email;

        using (var wb = new WebClient())
        {
            string url = BaseUrlNoTrailingSlash + Url.RouteUrl("DefaultApi", new { httproute = "", controller = "Emailer" });
             var response = wb.UploadValues(url, "POST", data.ToNameValueCollection());
        }

        return View();
    }

And here is what I am getting in my Post web api controller if I declare an httpContext variable like this:

var httpContext = (System.Web.HttpContextWrapper)Request.Properties["MS_HttpContext"];

httpContext.Request.Form = {ViewName=EmailTest&From=fromaddress%40yahoo.com&To=toaddress%40gmail.com&Fullname=John+Smith&Url=www.mysite.com}

httpContext.Request.Form is a System.Collections.Specialized.NameValueCollection {System.Web.HttpValueCollection}

2
  • Can you add examples of how you are Posting the data to the API? Commented Mar 7, 2014 at 9:32
  • @JonSusiak Please see the UPDATE section in my original post. I just added the information you are asking me. Commented Mar 7, 2014 at 12:13

1 Answer 1

3

I finally found the answer to my question here:

Web API Form Data Collection

The solution is to use FormDataCollection:

    public void Post([FromBody]FormDataCollection formData)
    {
        ...
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.