Post Frombody Always Null

FromBody in ASP.NET Core API returns null

You need to change all of your fields in the SupplierMaterialMaintenance class to the properties with getter and setter

public class SupplierMaterialMaintenance : IComparable<SupplierMaterialMaintenance>, IEquatable<SupplierMaterialMaintenance>
{
public Guid SupplierMaterialAssociationGuid { get; set; }
public int MinimumOrderQuantity { get; set; }
public Guid UnitOfMeasurementGuid { get; set; }
public int ConversionFactor { get; set; }
//goes like this...

You can take a look at Microsoft Documentation about Model Binding in .net-core

FromBody string parameter is giving null

By declaring the jsonString parameter with [FromBody] you tell ASP.NET Core to use the input formatter to bind the provided JSON (or XML) to a model. So your test should work, if you provide a simple model class

public class MyModel
{
public string Key {get; set;}
}

[Route("Edit/Test")]
[HttpPost]
public void Test(int id, [FromBody] MyModel model)
{
... model.Key....
}

and a sent JSON like

{
key: "value"
}

Of course you can skip the model binding and retrieve the provided data directly by accessing HttpContext.Request in the controller. The HttpContext.Request.Body property gives you the content stream or you can access the form data via HttpContext.Request.Forms.

I personally prefer the model binding because of the type safety.

Asp.net core FromBody always getting NULL

when i am trying to call this web service using webrequest on service side frombody always getting NULL if i am sending only file number and doc type i am getting data in from body but when i am trying to send pdf data from body getting NULL.

If you'd like to post pdf data with other information from request body to you backend service, you can refer to the following example.

//...

string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
string yourdata = System.Text.Json.JsonSerializer.Serialize(
new
{
DocType = doctype,
FileNumber = txtid.Text,
DocumentBody = base64String
});

writer.Write(yourdata);
writer.Close();

//...

The ByteArrayModelBinder can help bind data to byte[] DocumentBody property well.

public class ReceiveNoteDataReq
{
[ModelBinder(BinderType = typeof(ByteArrayModelBinder))]
public byte[] DocumentBody { get; set; }
public string DocType { get; set; }
public string FileNumber { get; set; }
}

Test Result

Sample Image

Besides, as @SabirHossain mentioned, you can try to modify your code to upload file through FormData, and this SO thread discussed similar requirement, you can refer to it.



Related Topics



Leave a reply



Submit