简体   繁体   中英

Asp.net core FromBody always getting NULL

Hello friends i have created Asp.net core web service and calling this web service form web application. in this web api user upload pdf and document type and File number. this is my asp.net core controller

[HttpPost ]
[Route("v1/ReceiveNoteData")]
public string   ReceiveNoteData([FromBody] ReceiveNoteDataReq request)
{
   return _IReceiveNoteData.ReceiveNoteDataResponse(request);        
}

ReceiveNoteDataReq class

public class ReceiveNoteDataReq
{
    public byte[] DocumentBody { get; set; }
    public string DocType { get; set; }
    public string FileNumber { get; set; }
}

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.

this is how i am calling the web service in my web application.

ReceiveNoteDataReq req = new ReceiveNoteDataReq()
req.DocumentBody = bytes;// pdf byte data 
req.FileNumber = txtid.Text;
req.DocType = doctype;
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string url = "http://localhost:905/v1/ReceiveNoteData";
CookieContainer cookieJar = new CookieContainer();

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "POST";
webRequest.ContentType = "application/json";
webRequest.MediaType = "application/json";
webRequest.Accept = "application/json";
webRequest.CookieContainer = cookieJar;
webRequest.KeepAlive = true;
webRequest.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";

webRequest.Credentials = CredentialCache.DefaultCredentials;
StreamWriter writer = new StreamWriter(webRequest.GetRequestStream());
JavaScriptSerializer jss = new JavaScriptSerializer();
string yourdata = jss.Serialize(req); // here i am converting class to json string.
writer.Write(yourdata);
writer.Close();


var myWebResponse = webRequest.GetResponse();
var responseStream = myWebResponse.GetResponseStream();
if (responseStream != null)
{
   var myStreamReader = new StreamReader(responseStream, Encoding.Default);
   result = myStreamReader.ReadToEnd();
}

Thanks in advance.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM