The requested resource does not support HTTP method ‘GET’

Please use the attributes from the System.Web.Http namespace on your WebAPI actions: [System.Web.Http.AcceptVerbs(“GET”, “POST”)] [System.Web.Http.HttpGet] public string Auth(string username, string password) {…} The reason why it doesn’t work is because you were using the attributes that are from the MVC namespace System.Web.Mvc. The classes in the System.Web.Http namespace are for WebAPI.

How to create ASP.NET Web API Url?

The ApiController has a property called Url which is of type System.Web.Http.Routing.UrlHelper which allows you to construct urls for api controllers. Example: public class ValuesController : ApiController { // GET /api/values public IEnumerable<string> Get() { // returns /api/values/123 string url = Url.Route(“DefaultApi”, new { controller = “values”, id = “123” }); return new string[] { … Read more

How to return raw string with ApiController?

You could have your Web Api action return an HttpResponseMessage for which you have full control over the Content. In your case you might use a StringContent and specify the correct content type: public HttpResponseMessage Get() { return new HttpResponseMessage() { Content = new StringContent( “<strong>test</strong>”, Encoding.UTF8, “text/html” ) }; } or public IHttpActionResult Get() … Read more

Return HTML from ASP.NET Web API

ASP.NET Core. Approach 1 If your Controller extends ControllerBase or Controller you can use Content(…) method: [HttpGet] public ContentResult Index() { return base.Content(“<div>Hello</div>”, “text/html”); } ASP.NET Core. Approach 2 If you choose not to extend from Controller classes, you can create new ContentResult: [HttpGet] public ContentResult Index() { return new ContentResult { ContentType = “text/html”, … Read more

Where is HttpContent.ReadAsAsync?

It looks like it is an extension method (in System.Net.Http.Formatting): HttpContentExtensions Class Update: PM> install-package Microsoft.AspNet.WebApi.Client According to the System.Net.Http.Formatting NuGet package page, the System.Net.Http.Formatting package is now legacy and can instead be found in the Microsoft.AspNet.WebApi.Client package available on NuGet here.

How to authorize CORS preflight request on IIS with Windows Authentication

There are several ways to accomplish this, other answers can be found on this similar question –> Angular4 ASP.NET Core 1.2 Windows Authentication CORS for PUT and POST Gives 401 CORS Module It is possible to configure IIS by using the CORS Module. As seen here: https://blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module And further information available here: https://blogs.iis.net/iisteam/getting-started-with-the-iis-cors-module The IIS … Read more

Compress HTTP GET Response

The easiest is to enable compression directly at IIS level. If you want to do it at the application level you could write a custom delegating message handler as shown in the following post: public class CompressHandler : DelegatingHandler { protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>((responseToCompleteTask) => { HttpResponseMessage response … Read more

Multiple Controller Types with same Route prefix ASP.NET Web Api

Web API (1.x-2.x) does not support multiple attribute routes with the same path on different controllers. The result is a 404, because all the route matches more than one controller and at that point Web API will consider the result ambiguous. Note that MVC Core does support this scenario note: MVC Core serves as both … Read more

Reading FromUri and FromBody at the same time

A post body is typically a URI string like this: Message=foobar&TestingMode=true You have to make sure that the HTTP header contains Content-Type: application/x-www-form-urlencoded EDIT: Because it’s still not working, I created a full example myself. It prints the correct data. I also used .NET 4.5 RC. // server-side public class ValuesController : ApiController { [HttpPost] … Read more