用户类
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace OdataProjectTest.Models
{
public class User
{
public int Id { get; set; }
public String FacebookId { get;set; }
public String Name { get; set; }
public DateTimeOffset? RegisterDate { get; set; }
public int Wins { get; set; }
public int Loses { get; set; }
public int GameCount { get; set; }
public ICollection<Question> Questions { get; set; }
}
}
用户控制器
using OdataProjectTest.Models;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
namespace OdataProjectTest.Controllers
{
public class UsersController : ODataController
{
Context db = new Context();
private bool UserExists(int key)
{
return db.Users.Any(p => p.Id == key);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
// get
[EnableQuery]
public IQueryable<User> Get()
{
return db.Users;
}
[EnableQuery]
public SingleResult<User> Get([FromODataUri] int key)
{
IQueryable<User> result = db.Users.Where(p => p.Id == key);
return SingleResult.Create(result);
}
//
// pentru add
public async Task<IHttpActionResult> Post(User user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// user.RegisterDate = new System.DateTimeOffset();
db.Users.Add(user);
await db.SaveChangesAsync();
return Created(user);
}
//
// pentru edit
public async Task<IHttpActionResult> Patch([FromODataUri] int key, Delta<User> user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var entity = await db.Users.FindAsync(key);
if (entity == null)
{
return NotFound();
}
user.Patch(entity);
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(entity);
}
//
// delete
public async Task<IHttpActionResult> Delete([FromODataUri] int key)
{
var user = await db.Users.FindAsync(key);
if (user == null)
{
return NotFound();
}
db.Users.Remove(user);
await db.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent);
}
//
}
}
我做了一些调试,当它到达时
public async Task<IHttpActionResult> Post(User user)
{
// here the user is null, but the modelstate.isvalid is true
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
// user.RegisterDate = new System.DateTimeOffset();
db.Users.Add(user);
await db.SaveChangesAsync();
return Created(user);
}
请求:
POST localhost..../Users HTTP/1.1
Accept: application/json;odata.metadata=full
Content-Type: application/json;odata.metadata=full
OData-MaxVersion: 4.0
OData-Version: 4.0
Transfer-Encoding: chunked
Host: localhost..blabla
Connection: Keep-Alive
User-Agent: Apache-Olingo/4.0.0-beta-01
104
{"@odata.type":"#OdataProjectTest.Models.User","FacebookId@odata.type":"String","FacebookId":"maicata","Name@odata.type":"String","Name":"sme","Wins@odata.type":"Int32","Wins":1,"Loses@odata.type":"Int32","Loses":1,"GameCount@odata.type":"Int32","GameCount":1}
0
答案 0 :(得分:2)
如果在用户参数上添加[FromBody]属性,它是否有效?
public async Task<IHttpActionResult> Post([FromBody] User user)
{
...
}