我正在尝试使用web api服务更新相册的值,但是当我使用put请求时它会到达控制器(有时),创建上下文,但是后来找不到/运行PUT方法我有。
我正在发送类Album(下面),格式化为JSON到Web API。
public class Album
{
[ScaffoldColumn(false)]
public int Id { get; set; }
[Required]
public string Title { get; set; }
public string Description { get; set; }
[Required]
public string Artist { get; set; }
public string Genre { get; set; }
[Required]
public int Year { get; set; }
[Display(Name="No. Tracks")]
public int NumberOfTracks { get; set; }
}
这是应该全部接收的WebAPI控制器
public class AlbumsController : ApiController
{
private AlbumsContext db = new AlbumsContext();
// GET api/Albums
public IQueryable<Album> Get()
{
return (from a in db.Albums select a);
}
public IQueryable<Album> Get(int skip, int page)
{
var ret = (from a in db.Albums select a);
ret = ret.OrderBy(a => a.Title);
ret = ret.Skip(skip);
ret = ret.Take(page);
return ret;
}
// GET api/Albums/5
public Album Get(int id)
{
return db.Albums.FirstOrDefault(a => a.Id == id);
}
// PUT api/Albums
[HttpPut]
public HttpResponseMessage Put([FromBody]Album album)
{
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
db.Entry(album).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
// POST api/Albums
public HttpResponseMessage Post(Album album)
{
if (ModelState.IsValid)
{
db.Albums.Add(album);
db.SaveChanges();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.Id }));
return response;
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
// DELETE api/Albums/5
public HttpResponseMessage Delete(int id)
{
Album album = db.Albums.Find(id);
if (album == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
db.Albums.Remove(album);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK, album);
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
这是发出PUT请求的服务器控制器方法
[HttpPut]
public ActionResult Edit(Album album, HttpPostedFileBase Image1, HttpPostedFileBase Image2, HttpPostedFileBase Image3, int AlbumID, int RemoveID = 0)
{
string AlbumApiUri = "http://localhost:58431/api/Albums";
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri(AlbumApiUri);
request.Method = HttpMethod.Put;
MediaTypeFormatter jsonFormatter = new JsonMediaTypeFormatter();
request.Content = new ObjectContent<Album>(album, jsonFormatter);
request.Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
album.Id = AlbumID;
HttpClient client = new HttpClient();
var response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
bool a = true;
}
else
{
bool b = true;
}
return RedirectToAction("Details", new { ID = AlbumID });
}
答案 0 :(得分:2)
事实证明,我几乎是个白痴,这引发问题的主要原因是我没有将Album标记为DataContract并包含DataMember字段。希望这能教会犯同样错误的人!