【发布时间】:2017-09-19 13:50:03
【问题描述】:
我正在尝试将用户对象从 c# 客户端应用程序发布到 web api,该 web api 应该将新用户存储到数据库中。该模型基于同一类。目前客户端这个函数用于发帖:
public static async Task CreateNewUser(string userName, string eMail, string password, string name, string firstName)
{
User newUser = new User(userName, eMail, password, name, firstName);
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(restAddress);
try
{
var response = await client.PostAsync(restAddress + "api/Users", newUser);
response.EnsureSuccessStatusCode();
}
catch (Exception e)
{
ExecutionConsole.WriteError(e.ToString());
}
}
}
而Web API中的控制器有这个功能:
// POST: api/Users
[ResponseType(typeof(User))]
public async Task<IHttpActionResult> PostUser(User user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Users.Add(user);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (UserExists(user.Username))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = user.Username }, user);
}
API 返回 400(错误请求)。 Web API 目前有这样的配置:
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
编辑:
用户模型:
public class User
{
public User(string userName, string email, string password) : this(userName, email, password, null, null)
{
}
public User(string userName, string email, string password, string Name, string firstName)
{
this.Username = userName;
this.Email = email;
this.Password = password;
this.Name = Name;
this.firstName = firstName;
}
[Key, Required]
public string Username { get; set; }
public string Name { get; set; }
public string firstName { get; set; }
[Required]
public string Email { get; set; }
[Required]
public string Password { get; set; }
public virtual ICollection<Playlist> CreatedPlaylists { get; set; }
public virtual ICollection<Track> SavedTracks { get; set; }
}
HTTPRequest(Rest API 是我的 Web API 项目的名称):
{Method: POST, RequestUri: 'http://localhost:2468/api/Users', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[RestAPI.Model.User, RestAPI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], Headers:
{
Content-Type: application/json; charset=utf-8
Content-Length: 143
}}
Content: {System.Net.Http.ObjectContent<RestAPI.Model.User>}
Headers: {}
Method: {POST}
Properties: Count = 0
RequestUri: {http://localhost:2468/api/Users}
Version: {1.1}
HTTP 内容:
{System.Net.Http.ObjectContent<RestAPI.Model.User>}
Formatter: {System.Net.Http.Formatting.JsonMediaTypeFormatter}
Headers: {Content-Type: application/json; charset=utf-8
Content-Length: 143
}
ObjectType: {Name = "User" FullName = "RestAPI.Model.User"}
Value: {RestAPI.Model.User}
编辑 #2:
响应正文:
Id = 22,
Status = RanToCompletion,
Method = "{null}",
AsyncState: null
CancellationPending: false
CreationOptions: None
Exception: null
Id: 22
Result: {Message:"The request is invalid.",
ModelState:{"user.Username":["Unable to find a constructor to use for type ClAuP.RestAPI.Model.User. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'Username', line 1, position 12."]}}"
Status: RanToCompletion
【问题讨论】:
-
有效载荷可能有问题。你能显示
User类和正在发送的http 请求吗? -
你真的在控制器中接收到用户模型吗?是否为空?您能否提供状态码为 400 的响应正文?
-
@Merchezatter 收到的模型为空。
-
您的 User 类应该是 serializable 以便可以将其转换为字符串并在 http 中发送。这就是对象在应用程序之间传递的方式。查看 [Serializable] 属性和要序列化的特定字段。
标签: c# asp.net asp.net-web-api