c# - 如何在浏览器中向 ASP.NET (.NET FRAMEWORK) 中的 DELETE http 方法发出请求
问题描述
在laravel
或某些express.js
库中添加中间件以检查是否调用了一个字段,该字段_Method
将在服务器到达时覆盖 http 请求的实际 http 方法,因为浏览器仅支持POST
and GET
。
确实ASP.NET (.NET Framework)
包含我刚才提到的内置中间件,我在网上找不到任何东西。
如果是这样,我该如何使用它或者你知道这样的atricle。
如果没有,我如何自己创建这样的中间件。
我自己无法创建一个,因为HttpRequest
in the HttpContext
isreadonly
和 the HttpMethod
in the HttpRequest
is 也是readonly
。
preventDefault()
或者是使用和/或ajax
发送(例如)DELETE
请求的唯一选择javascript
?
解决方案
首先,您需要在控制器或 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');
}
});
推荐阅读
- dataframe - Julia DataFrames 从多列过滤
- ios - 在 iOS 14 上通过 Bonjour 发现服务
- bash - Bash 脚本错误/预期的整数表达式
- amazon-web-services - Route 53 aws www.domain.com 有效,但 domain.com 无效
- python - SymPy 简化阶乘表达式
- javascript - 从 Object 数组中,获取仅匹配键值的数组
- amazon-web-services - 需要 MFA 时如何在 CI/CD 命令中运行无服务器部署
- azure - 创建自包含部署时如何解决 Azure Devops Pipeline 中的目标问题
- javascript - 如何创建可选接口?
- flutter - Flutter TabBarView 和 Tabs