After a lot of research, I am posting this question as I couldn't figure it out myself.
The scenario is that I need to pass the following parameters to a HttpPost request in a Web API 2 controller in .Net framework 4.8:
- Date -> Date object
- Author -> string
- Title -> string
- Content -> string
- Image -> File object
My front end Angular code is:
saveNewsArticle(data) {
return new Observable(observer => {
let apiStr = this.apiStrBase + "SaveNewsArticle";
let formData = new FormData();
formData.append('date', data.publishDate);
formData.append('author', data.author);
formData.append('title', data.title);
formData.append('image', data.image);
formData.append('content', data.content);
this.httpClient.post(apiStr, formData).subscribe((result) => {
observer.next({ result: true, data: result });
},
error => {
observer.next({ result: false });
}
);
});
}
My C# code is:
[HttpPost]
public void SaveNewsArticle()
{
var date = HttpContext.Current.Request.Params["date"];
var author = HttpContext.Current.Request.Params["author"];
var title = HttpContext.Current.Request.Params["title"];
var content = HttpContext.Current.Request.Params["content"];
var image = HttpContext.Current.Request.Files.Count > 0 ? HttpContext.Current.Request.Files[0] : null;
}
All of this just works fine, but, I'm using a WYSIWYG editor to get the styling of the text and hence the content is html, for example, <p>this is my content</p> and I get this error in c#:
System.Web.HttpRequestValidationException: A potentially dangerous Request.Form value was detected from the client
Following is what I have tried so far:
I don't want to use
<httpRuntime requestValidationMode="2.0" />I can pass it as a model
public void SaveArticle(Article article)which gets everything just fine but the image is alwaysnull.I can replace the '<' and '>' and that solves the problem for me but that is not how this should be done. It's a hack.
In MVC we could just use
[ValidateInput(false)]or[AllowHtml]but can't use it onWeb API 2.Should I implement a custom formatter to handle the
multipart/form-datalike done here
Any help/guidance is greatly appreciated.

Articleclass? How are you trying to access theimageif you usearticleas Post Body in Action method?IFormFilewhich is part of.NetCoreand I'm using.Net framework. Please do not vote to close this answer. If it was that easy I wouldn't have posted the question in the first place.MediaTypeFormatteravailable? Are you able to upload JUST the image? If not, then there's a config or formatter issue in regards tomultipart/form-datatype requests.