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
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
Regular Expression for Valid Filename
How to Check If Multiple Values in an Array Are Equal to One String
Calculate a Checksum for a String
How to Generate Unique Number of 8 Digits
How Avoid Adding Duplicates to Database Managed by Entityframework Caused by Seed Method
Instantiating an Iformfile from a Physical File
Generating Aes 256 Bit Key Value
Pass an Array of Integers to ASP.NET Web API
Download File from an ASP.NET Web API Method Using Angularjs
How to Pass Jquery Variable Value to C# MVC
How to Iterate Though Each Child Node in an Xml File
Response to Preflight Request Doesn't Pass Access Control Check (Angular2)
A Better Way to Use Automapper to Flatten Nested Objects
Accessing a Shared File (Unc) from a Remote, Non-Trusted Domain With Credentials
Json.Net: Deserilization With Double Quotes