简体   繁体   English

Asp.net 核心 FromBody 总是得到 NULL

[英]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.

相关问题 在asp.net核心中调用asp.net mvc 5 Web api,FromBody始终为null - calling asp.net mvc 5 web api in asp.net core, FromBody always null 通过[FromBody]到MongoDB GeoJsonObjectModel成员的POST / PUT到ASP.Net Core始终为null - POST/PUT to ASP.Net Core with [FromBody] to a MongoDB GeoJsonObjectModel member is always null 在 ASP.Net Core 5 MVC 控制器中,当传递包含小数的 JSON 对象 FromBody 时,模型始终为空 - In ASP.Net Core 5 MVC Controller, when passed a JSON object FromBody that contains a decimal the model is always null FromBody 在 ASP.NET 核心 API 返回 null - FromBody in ASP.NET Core API returns null 与 ASP.NET Core 中的 FromBody 混淆 - Confused with FromBody in ASP.NET Core ASP.NET 核心 - [FromBody] 的奇怪行为 - ASP.NET Core - weird behavior with [FromBody] 消耗asp.net web api的android改造[FromBody]始终为null - android retrofit consuming asp.net web api [FromBody] always null 发布到 ASP.NET Core 3.1 Web 应用程序时,“[FromBody]MyClass 数据”通常为空 - When posting to an ASP.NET Core 3.1 web app, "[FromBody]MyClass data" is often null 是否可以在 ASP.NET Core 中结合使用 [FromRoute] 和 [FromBody]? - Is it possible to combine [FromRoute] and [FromBody] in ASP.NET Core? 在ASP.NET CORE中将属性路由值转换为Model / FromBody参数 - Attribute Routing Values into Model/FromBody Parameter in ASP.NET CORE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM