3

I am in need of migrating from normal MVC controller to WebApi controller.

My normal mvc controller.

[HttpPost]
[ValidateInput(false)]
public ActionResult Validate(string data)
{

}

WebApi Controller

[HttpPost]
[ValidateInput(false)] 
// the moment I resolve the namespace System.Web.Mvc all the [HttpPost],[HttpGet] starts throwing error.
public HttpStatusCode Post([FromBody]string data)
{

}

Can somebody explain why so & how to address this requirement?

Kgn-web
  • 7,047
  • 24
  • 95
  • 161

3 Answers3

0

To have an Api Controller, you'll have to inherit from ApiController. Also, if by adding the Namespace you start getting errors, then you will have to add the namespace explicitly to the attributes like this for example. System.Web.Http.HttpPost.

Alf Moh
  • 7,159
  • 5
  • 41
  • 50
0

You don't really have to use something like [ValidateInput(false)] in your web api, it's meant to prevent XSS attacks in MVC applications.

Also, the namespace you have to refer is System.Web.Http.

Jose Francis
  • 950
  • 13
  • 28
  • https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/model-validation-in-aspnet-web-api – Egli Becerra Jun 29 '20 at 15:18
0

I used htmlEncode in javascript from here and HttpUtility.HtmlDecode in C#

Stanislav
  • 4,389
  • 2
  • 33
  • 35