我正在尝试使用 Web api 服务更新专辑的值,但是当我使用 put 请求时,它会到达控制器(有时),创建上下文,但是然后找不到/运行我拥有的 PUT 方法.
我正在向 Web API 发送格式为 JSON 的类专辑(如下)。
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 });
}