C# 发送JSON+;通过邮递员归档
我有一个Android应用程序,它将以下多部分内容发布到我的应用程序中。该请求包含JSON以及一个图像文件C# 发送JSON+;通过邮递员归档,c#,asp.net,asp.net-web-api,asp.net-core-webapi,multipartform-data,C#,Asp.net,Asp.net Web Api,Asp.net Core Webapi,Multipartform Data,我有一个Android应用程序,它将以下多部分内容发布到我的应用程序中。该请求包含JSON以及一个图像文件 AndroidNetworking.upload(baseUrl+"updateuserprofile") .addMultipartFile("UserImage",userImage) .setTag("UserImage") .addMu
AndroidNetworking.upload(baseUrl+"updateuserprofile")
.addMultipartFile("UserImage",userImage)
.setTag("UserImage")
.addMultipartParameter("PhoneNumber",phone)
.addMultipartParameter("UserId",userId)
.addMultipartParameter("firstName",firstName)
.addMultipartParameter("LastName",lastName)
.setPriority(Priority.HIGH)
.setOkHttpClient(okHttpClient)
.build();
我的应用程序是ASP.NET WebAPI 2.0应用程序,我不知道如何EditProfile
参数,该参数总是NULL
public async Task<IHttpActionResult> EditProfile ([FromBody] EditProfile profile)
{
// `profile` is populated if only JSON is sent.
// `profile` is `NULL` if multi-part content is sent.
}
public class EditProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string MobileNo { get; set; }
public long UserId { get; set; }
public string ImageString { get; set; }
public HttpPostedFileBase PostedFile { get; set; }
}
public异步任务EditProfile([FromBody]EditProfile)
{
//如果只发送JSON,则填充'profile'。
//如果发送多部分内容,“profile”为“NULL”。
}
公共类编辑配置文件
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串电子邮件{get;set;}
公共字符串MobileNo{get;set;}
公共长用户标识{get;set;}
公共字符串ImageString{get;set;}
公共HttpPostedFileBase PostedFile{get;set;}
}
如果您有任何建议,我们将不胜感激。当您发送一个由多个部分组成的内容时,它不是json,而是表单数据。因此,您应该使用
[FromFrom]
而不是使用[FromFrom]
。([FromForm]
是ASP.NETCore的一项功能,因为您使用的是完整的框架,它不适合您)
我尝试了两种方法
[FromForm]
需要依赖于AspNetCore
,这对我来说是不可能的。下一个带有HttpPostedFileBase
的示例抛出了一个InvalidOperationException
:无法将多个参数(“editProfile”和“postedFile”)绑定到请求的内容。抱歉。第一个使用[FromForm]
的只能在asp.net内核上使用。我将更新答案以澄清问题。在第二个示例中,您是否从模型中删除了HttpPostedFileBase?作为模型的一部分,在使用和不使用HttpPostedFileBase
的情况下进行了尝试。现在的错误是:无法将多个参数(“editProfile”和“postedFile”)绑定到请求的内容。
。
public async Task<IHttpActionResult> EditProfile ([FromForm] EditProfile profile)
{
// `profile` will not be populated if JSON is sent.
// `profile` will be populated if multi-part content is sent.
}
public class EditProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string MobileNo { get; set; }
public long UserId { get; set; }
public string ImageString { get; set; }
public HttpPostedFileBase PostedFile { get; set; }
}
public async Task<IHttpActionResult> EditProfile (EditProfile profile, HttpPostedFileBase postedFile)
{
// `profile` should be populated with a JSON or multi-part content
// `postedFile` will be populated only if multi-part content is sent with a file.
}
public class EditProfile
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string MobileNo { get; set; }
public long UserId { get; set; }
public string ImageString { get; set; }
// REMOVE HttpPostedFileBase property
}