我正在使用视图上传照片,然后在控制器中将其作为HttpPostedFileBase
接收。
然后我将其发送到Http请求中,一切都很顺利,但是当我以json对象接收Http请求时,照片为空。
以下是请求:
public ActionResult RegisterUsersCheck(User_all_data user_data)
{
var webAddr = "http://localhost:59305/api/User/RegisterUsersCheck";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr);
httpWebRequest.ContentType = "application/json; charset=utf-8";
httpWebRequest.Accept = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{ user : { 'FirstName':'" + user_data.user.FirstName +
"','LastName': '" + user_data.user.LastName + "','Email': '" + user_data.user.Email +
"','Password': '" + user_data.user.Password + "','Country': '" + user_data.user.Country +
"','PhoneNumber': '" + user_data.ProfilePicture +
"'},'ProfilePicture': '" + user_data.PhoneNumber + "'}";
streamWriter.Write(json);
streamWriter.Flush();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
return RedirectToAction("Index");
}
这里是我如何接收请求的方式
public bool RegisterUsersCheck ([FromBody] User_all_data user_ext)
{
List<User> all_users = repo.getUsers();
var match = all_users.Find(i => i.Email == user_ext.user.Email);
if (match == null)
{
RegisterUsers(user_ext);
return true;
}
else
{
return false;
}
}
以下是 User_all_data
类的代码:
public class User_all_data
{
public User user { get; set; }
public HttpPostedFileBase ProfilePicture { get; set; }
}
正如我之前提到的,发送方没问题,但是当我收到请求时,ProfilePicture
为空。