gpt4 book ai didi

jquery - WebApi HttpDelete 的困境

转载 作者:行者123 更新时间:2023-12-01 04:47:53 27 4
gpt4 key购买 nike

我有一个像这样的 Controller (我将发布所有代码,以便您可以看到它是如何工作的)。

/// <summary>
/// Handles all of the Upload functions, including GetAll, Get and Create.
/// </summary>
[RoutePrefix("Api/Uploads")]
public class UploadsController : BaseController
{
private readonly UploadService service;

/// <summary>
/// Parameterless constructor in which the upload service is instantiated.
/// </summary>
public UploadsController()
{
this.service = new UploadService(UnitOfWork);
}

// GET api/uploads
/// <summary>
/// Gets a list of all the Uploads.
/// </summary>
/// <returns>A list of uploads.</returns>
[HttpGet]
[Route("")]
[ResponseType(typeof(IList<UploadViewModel>))]
public async Task<IHttpActionResult> Get()
{
try
{
var uploads = await this.service.GetAllAsync();
var models = uploads.Select(model => new UploadViewModel(model)).ToList();

return Ok(models);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}

// GET api/uploads
/// <summary>
/// Gets an upload by the required parameter; id.
/// </summary>
/// <param name="id">The required id paramter of the upload.</param>
/// <returns>An upload view model.</returns>
[HttpGet]
[Route("{id:int}")]
[ResponseType(typeof(UploadViewModel))]
public async Task<IHttpActionResult> Get(int id)
{
try
{
var model = new UploadViewModel(await this.service.GetAsync(id));

return Ok(model);
}
catch (Exception ex)
{
return InternalServerError(ex);
}
}

// POST api/uploads
/// <summary>
/// Creates an Upload.
/// </summary>
/// <param name="model">The model representing the upload.</param>
/// <returns>Nothing.</returns>
[HttpPost]
[Route("")]
[Authorize]
public async Task<IHttpActionResult> Post(UploadBindingViewModel model)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);

var upload = new Upload
{
Id = model.Id,
Name = model.Name,
Url = model.Url
};

try
{
this.service.Create(upload);

await this.UnitOfWork.SaveChangesAsync();
}
catch (Exception ex)
{
return InternalServerError(ex);
}

return Ok(upload.Id);
}

// DELETE api/uploads
/// <summary>
/// Deletes an upload.
/// </summary>
/// <param name="id">The id of the upload.</param>
/// <returns></returns>
[HttpDelete]
[Route("{id:int}")]
[Authorize]
public async Task<IHttpActionResult> Delete(int id)
{
try
{
await this.service.RemoveAsync(id);

await this.UnitOfWork.SaveChangesAsync();
}
catch (Exception ex)
{
return InternalServerError(ex);
}

return Ok();
}
}

在 Controller 的底部,我有删除功能,目前如下所示:

// DELETE api/uploads
/// <summary>
/// Deletes an upload.
/// </summary>
/// <param name="id">The id of the upload.</param>
/// <returns></returns>
[HttpDelete]
[Route("{id:int}")]
[Authorize]
public async Task<IHttpActionResult> Delete(int id)
{
try
{
await this.service.RemoveAsync(id);

await this.UnitOfWork.SaveChangesAsync();
}
catch (Exception ex)
{
return InternalServerError(ex);
}

return Ok();
}

现在,如果我像这样从 jQuery 调用它:

$.ajax({
type: "DELETE",
url: uploadUrl + "?id=" + self.id()
}).fail(function () {
toastr.error("Failed to remove the upload from our system.");
});

或者像这样:

$.ajax({
type: "DELETE",
url: uploadUrl,
data: { id: self.id() }
}).fail(function () {
toastr.error("Failed to remove the upload from our system.");
});

我收到此错误:

{"Message":"The requested resource does not support http method 'DELETE'."}

但是,如果我将 Controller 方法更改为:

// DELETE api/uploads
/// <summary>
/// Deletes an upload.
/// </summary>
/// <param name="id">The id of the upload.</param>
/// <returns></returns>
[HttpDelete]
[Route("")]
[Authorize]
public async Task<IHttpActionResult> Delete(int id)
{
try
{
await this.service.RemoveAsync(id);

await this.UnitOfWork.SaveChangesAsync();
}
catch (Exception ex)
{
return InternalServerError(ex);
}

return Ok();
}

然后像这样使用它:

$.ajax({
type: "DELETE",
url: uploadUrl + "?id=" + self.id()
}).fail(function () {
toastr.error("Failed to remove the upload from our system.");
});

它有效,但如果我更改为:

$.ajax({
type: "DELETE",
url: uploadUrl,
data: { id: self.id() }
}).fail(function () {
toastr.error("Failed to remove the upload from our system.");
});

我收到与以前相同的错误消息。

我想使用后一个语句调用 Web API,并将 {id:int} 声明保留在我的 Controller 中。有人可以告诉我我做错了什么吗?

最佳答案

我现在可以回答这个问题了。DELETE HttpMethod 不接受正文数据,它只能接受查询字符串参数,这就是附加 URL 方法起作用的原因。

此外,Web API 方法的默认参数已经是 int,因此 Route 应指定为 [Route("")] 。这意味着调用的实际路径应该是使用 DELETE HttpMethod 的 /Api/Uploads

我希望这已经为遇到同样问题的其他人解决了这个问题。

关于jquery - WebApi HttpDelete 的困境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26525035/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com