-
-
Save morsdyce/7989590 to your computer and use it in GitHub Desktop.
Web Api Remove the need for [FromBody] and [FromURI] #asp.net #mvc #webapi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FormHandler : DelegatingHandler | |
{ | |
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) | |
{ | |
if (request.Content != null && request.Content.Headers.ContentType != null && | |
request.Content.Headers.ContentType.MediaType == "application/x-www-form-urlencoded") | |
{ | |
var routeData = request.Properties["MS_HttpRouteData"] as IHttpRouteData; | |
var form = request.Content.ReadAsFormDataAsync().Result; | |
foreach (var key in form.AllKeys) | |
{ | |
routeData.Values.Add(new KeyValuePair<string, object>(key, form[key])); | |
} | |
} | |
return base.SendAsync(request, cancellationToken); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FormHandler : DelegatingHandler | |
{ | |
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) | |
{ | |
if (request.Content != null && request.Content.Headers.ContentType != null && | |
request.Content.Headers.ContentType.MediaType == "application/x-www-form-urlencoded") | |
{ | |
var routeData = request.Properties["MS_HttpRouteData"] as IHttpRouteData; | |
var form = request.Content.ReadAsFormDataAsync().Result; | |
foreach (var key in form.AllKeys) | |
{ | |
routeData.Values.Add(new KeyValuePair<string, object>(key, form[key])); | |
} | |
} | |
return base.SendAsync(request, cancellationToken); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment