如果调用.net core Web API不能发送PUT/DELETE请求怎么办?
通过阅读大佬的文章 http://www.cnblogs.com/artech/p/x-http-method-override.html
想到的 通过注册中间件来解决这个问题
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseHttpMethodOverride();
}
public static class HttpMethodOverrideExtensions
{
/// <summary>
/// Allows incoming POST request to override method type with type specified in header.
/// </summary>
/// <param name="builder">The <see cref="T:Microsoft.AspNetCore.Builder.IApplicationBuilder" /> instance this method extends.</param>
public static IApplicationBuilder UseHttpMethodOverride(this IApplicationBuilder builder)
{
if (builder == null)
throw new ArgumentNullException(nameof (builder));
return builder.UseMiddleware<HttpMethodOverrideMiddleware>(Array.Empty<object>());
}
}
public class HttpMethodOverrideMiddleware
{
private const string xHttpMethodOverride = "X-Http-Method-Override";
private readonly RequestDelegate _next;
private readonly HttpMethodOverrideOptions _options; public HttpMethodOverrideMiddleware(RequestDelegate next, IOptions<HttpMethodOverrideOptions> options)
{
if (next == null)
throw new ArgumentNullException(nameof (next));
if (options == null)
throw new ArgumentNullException(nameof (options));
this._next = next;
this._options = options.Value;
} public async Task Invoke(HttpContext context)
{
if (string.Equals(context.Request.Method, "POST", StringComparison.OrdinalIgnoreCase))
{
if (this._options.FormFieldName != null)
{
if (context.Request.HasFormContentType)
{
StringValues stringValues = (await context.Request.ReadFormAsync(new CancellationToken()))[this._options.FormFieldName];
if (!string.IsNullOrEmpty((string) stringValues))
context.Request.Method = (string) stringValues;
}
}
else
{
StringValues header = context.Request.Headers["X-Http-Method-Override"];
if (!string.IsNullOrEmpty((string) header))
context.Request.Method = (string) header;
}
}
await this._next(context);
}
}
如果调用.net core Web API不能发送PUT/DELETE请求怎么办?的更多相关文章
- 如果调用ASP.NET Web API不能发送PUT/DELETE请求怎么办?
理想的RESTful Web API采用面向资源的架构,并使用请求的HTTP方法表示针对目标资源的操作类型.但是理想和现实是有距离的,虽然HTTP协议提供了一系列原生的HTTP方法,但是在具体的网络环 ...
- .NET Core WEB API中接口参数的模型绑定的理解
在.NET Core WEB API中参数的模型绑定方式有以下表格中的几种: 微软官方文档说明地址:https://docs.microsoft.com/zh-cn/aspnet/core/web-a ...
- Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 下
一,引言 上一节讲到如何在我们的项目中集成Azure AD 保护我们的API资源,以及在项目中集成Swagger,并且如何把Swagger作为一个客户端进行认证和授权去访问我们的WebApi资源的?本 ...
- .net core web api 与httpclient发送和接收文件及数据
客户端 HttpClient var url = $"https://localhost:44323/api/values/posttest?resource_source=yangwwme ...
- 在Mac下创建ASP.NET Core Web API
在Mac下创建ASP.NET Core Web API 这系列文章是参考了.NET Core文档和源码,可能有人要问,直接看官方的英文文档不就可以了吗,为什么还要写这些文章呢? 原因如下: 官方文档涉 ...
- 如何在ASP.NET Core Web API测试中使用Postman
使用Postman进行手动测试 如果您是开发人员,测试人员或管理人员,则在构建和使用应用程序时,有时了解各种API方法可能是一个挑战. 使用带有.NET Core的Postman为您的Web API生 ...
- ASP.NET Core Web API下事件驱动型架构的实现(一):一个简单的实现
很长一段时间以来,我都在思考如何在ASP.NET Core的框架下,实现一套完整的事件驱动型架构.这个问题看上去有点大,其实主要目标是为了实现一个基于ASP.NET Core的微服务,它能够非常简单地 ...
- ASP.NET Core Web API下事件驱动型架构的实现(二):事件处理器中对象生命周期的管理
在上文中,我介绍了事件驱动型架构的一种简单的实现,并演示了一个完整的事件派发.订阅和处理的流程.这种实现太简单了,百十行代码就展示了一个基本工作原理.然而,要将这样的解决方案运用到实际生产环境,还有很 ...
- ASP.NET Core Web API下事件驱动型架构的实现(三):基于RabbitMQ的事件总线
在上文中,我们讨论了事件处理器中对象生命周期的问题,在进入新的讨论之前,首先让我们总结一下,我们已经实现了哪些内容.下面的类图描述了我们已经实现的组件及其之间的关系,貌似系统已经变得越来越复杂了. 其 ...
随机推荐
- Harmonic Value Description HDU - 5916
The harmonic value of the permutation p1,p2,⋯pn is ∑i=1n−1gcd(pi.pi+1) Mr. Frog is wondering about t ...
- runltp<p-pan
- Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程
https://blog.csdn.net/jywowaa/article/details/52263711 学习中用到深度学习的框架,需要搭建caffe.theano和torch框架.经过一个月的不 ...
- 关于nginx配置虚拟主机
前提:我的虚拟主机的外网ip为111.231.226.228(是云服务器哈) 本地测试环境为windows7(修改本地的hosts文件) 步骤:(安装nginx可以看看我文章“linux ng ...
- android之进度条
xml引用 <ProgressBar android:id="@+id/pb_progressbar" style="@style/StyleProgressBar ...
- 在eclipse中, 如何快速输入(快捷键)System.out.println();
1.快速输入(快捷键)System.out.println(); 首先输入sysout或syso,然后ALT+/ System.out.println(); 2.快速输入(快捷键)System.err ...
- JavaScript中字符串的方法:charAt()、charCodeAt()、indexOf()、lastIndexOf()、substr()、slice()、substring()、search()、replace()、split()、concat()、toLowerCase()、toUpperCase()
1.字符创的创建: //1.通过new 来创建 var str = String("javascript"); //2.3.直接使用字面量进行创建 var str='html5'; ...
- 解决c1xx fatal error C1083 Cannot open source file
在项目开发过程中,遇到一个问题,一个工程B导入另外一个工程A的生产代码,出现这个错误,最后查阅资料发现是文件路径太深,导致文件路径字符超过了217字符. 写了一个测试Demo来验证: 一.新建Win3 ...
- Java1.7 HashMap 实现原理和源码分析
HashMap 源码分析是面试中常考的一项,下面一篇文章讲得很好,特地转载过来. 本文转自:https://www.cnblogs.com/chengxiao/p/6059914.html 参考博客: ...
- Spyder 调出绘图界面
Tools->Preference->IPython console->Graphics->Graphics backend->QT4 or QT5