[英]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.您好朋友,我创建了 Asp.net 核心 web 服务并调用此 web 服务表单 Z2567A5EC9705EB7AC2CZ8403 应用程序。 in this web api user upload pdf and document type and File number.
在此 web api 用户上传 pdf 和文档类型和文件号。 this is my asp.net core controller
这是我的 asp.net 核心 controller
[HttpPost ]
[Route("v1/ReceiveNoteData")]
public string ReceiveNoteData([FromBody] ReceiveNoteDataReq request)
{
return _IReceiveNoteData.ReceiveNoteDataResponse(request);
}
ReceiveNoteDataReq class 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. 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.这就是我在我的 web 应用程序中调用 web 服务的方式。
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.
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.如果您想将请求正文中的 pdf 数据与其他信息一起发布到您的后端服务,您可以参考以下示例。
//...
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. ByteArrayModelBinder可以帮助将数据很好地绑定到
byte[] DocumentBody
属性。
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.此外,正如@SabirHossain 提到的,您可以尝试修改您的代码以通过FormData 上传文件,这个SO线程讨论了类似的需求,您可以参考它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.