首页 > 解决方案 > 如何在浏览器中向 ASP.NET (.NET FRAMEWORK) 中的 DELETE http 方法发出请求

问题描述

laravel或某些express.js库中添加中间件以检查是否调用了一个字段,该字段_Method将在服务器到达时覆盖 http 请求的实际 http 方法,因为浏览器仅支持POSTand GET

确实ASP.NET (.NET Framework)包含我刚才提到的内置中间件,我在网上找不到任何东西。

如果是这样,我该如何使用它或者你知道这样的atricle。
如果没有,我如何自己创建这样的中间件。
我自己无法创建一个,因为HttpRequestin the HttpContextisreadonly和 the HttpMethodin the HttpRequestis 也是readonly

preventDefault()或者是使用和/或ajax发送(例如)DELETE请求的唯一选择javascript

标签: c#asp.netmiddleware.net-framework-versionhttp-method

解决方案


首先,您需要在控制器或 API 中将 [HttpDelete] 属性分配给您的函数。例如:

[HttpDelete]
public JsonResult MyDeleteFunction(int id)
{
    //Write your delete code here
    return Json("some responses");
}

然后在客户端,您可以使用 ajax 来执行删除请求。

$.ajax({
    url: '/ControllerName/MyDeleteFunction',
    dataType: "json",
    type: "DELETE",
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({id: 123}),
    async: true,
    processData: false,
    cache: false,
    success: function (data) {
        alert(data);
    },
    error: function (xhr) {
        alert('error');
    }
});

推荐阅读