Skip to content

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

Closed

Activity

  1. self-assigned this
    on Dec 13, 2017
  2. Marusyk commented on Dec 13, 2017

    @Marusyk
    OwnerAuthor

    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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions