Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support PUT with a header override for firewalls that don't support PATCH #5

Closed
Marusyk opened this issue Dec 4, 2017 · 1 comment
Assignees

Comments

@Marusyk
Copy link
Owner

Marusyk commented Dec 4, 2017

https://www.hanselman.com/blog/HTTPPUTOrDELETENotAllowedUseXHTTPMethodOverrideForYourRESTServiceWithASPNETWebAPI.aspx

@Marusyk
Copy link
Owner Author

Marusyk commented Dec 13, 2017

For POST instead of PATCH use HttpMethodOverrideMiddleware

public void Configure(IApplicationBuilder app)
{
    app.UseHttpMethodOverride();
    app.UseMvc();
}

For PUT instead of PATCH create custom middleware

public class MethodOverrideMiddleware
{
	private readonly RequestDelegate _next;

	readonly string[] _methods = { "DELETE", "PATCH", "PUT" };
	const string _header = "X-Http-Method-Override";

	public MethodOverrideMiddleware(RequestDelegate next)
	{
		_next = next;
	}

	public Task Invoke(HttpContext context)
	{
		if (HttpMethods.IsPut(context.Request.Method) && context.Request.Headers.ContainsKey(_header))
		{
			var method = context.Request.Headers[_header].ToString();
			if (_methods.Contains(method))
			{
				context.Request.Method = method;
			}
		}
		return _next.Invoke(context);
	}
}

and use it in Startup.cs

app.UseMiddleware<CustomMethodOverrideMiddleware>();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant