FromBody value get null

大兔子大兔子 提交于 2019-12-21 01:18:46

问题


This is Asp.Net Webform application
This is my POST method in my Apicontroller

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

I'm with fiddler post process.
I did so experiment.
But it did not.

What is the problem.
Can you help?

I've tried it, I've failed.

public void Post(MyViewModel model)
{
   string aa = model.Value;
}

public class MyViewModel
{
   public string Value { get; set; }
}

In Fiddler:

Request Body:   
Value=hakan

回答1:


The POST body payload in Fiddler should be:

=foo_bar

instead of:

value=foo_bar

That's just one of those strange things about the model binding in the Web API. If you want to support value=foo_bar in the POST body payload you could always write a view model:

public class MyViewModel
{
    public string Value { get; set; }
}

and then have your method take this view model as parameter:

public void Post(MyViewModel model)
{
    ... work with model.Value here as usual
}



回答2:


I meet this issue, and the solution is:
to fit the Request Body format =foo_bar,
also need Request haders:

Content-Type: application/x-www-form-urlencoded



回答3:


Web API Null Bug

For the default Post() method to work, POST data from Fiddler must appear as follows




回答4:


I had a similar experience with a WebAPI application recently. The application had been working correctly in my test environment, but when we ran a build off the build server the application failed to deserialize the object and always set the parameter following [FromBody] to null.

The root cause was the web.config and packages.config specifying Json.NET 6.0.4 and the NuGet package that was in the bin was 10.0.3.

The model binding kind of looks like a black box from the outside at first. Especially since it can quietly return a null with no indication as to what is wrong. On this Microsoft page under the heading Testing Object Serialization there is an example of how to test serialization and deserialization:

string Serialize<T>(MediaTypeFormatter formatter, T value)
{
    // Create a dummy HTTP Content.
    Stream stream = new MemoryStream();
    var content = new StreamContent(stream);
    /// Serialize the object.
    formatter.WriteToStreamAsync(typeof(T), value, stream, content, null).Wait();
    // Read the serialized string.
    stream.Position = 0;
    return content.ReadAsStringAsync().Result;
}

T Deserialize<T>(MediaTypeFormatter formatter, string str) where T : class
{
    // Write the serialized string to a memory stream.
    Stream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(str);
    writer.Flush();
    stream.Position = 0;
    // Deserialize to an object of type T
    return formatter.ReadFromStreamAsync(typeof(T), stream, null, null).Result as T;
}

// Example of use
void TestSerialization()
{
    var value = new Person() { Name = "Alice", Age = 23 };

    var xml = new XmlMediaTypeFormatter();
    string str = Serialize(xml, value);

    var json = new JsonMediaTypeFormatter();
    str = Serialize(json, value);

    // Round trip
    Person person2 = Deserialize<Person>(json, str);
}

When I put the above code in my application and ran it Visual Studio threw an exception in both the serialize and deserialize functions. The exception clearly stated that loading of Json.net version 6.0.4 had failed. Which led me to discover that version 10.0.3 was in the bin instead of 6.0.4. I right clicked on the project clicked manage NuGet Packages... in the context menu. Then removed and re-added the Json.net NuGet package. Doing that set the Json.net NuGet package in the bin and updated the packages.config and web.config with matching version numbers.



来源:https://stackoverflow.com/questions/16837844/frombody-value-get-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!