2

I've been trying to get Web Api to work with Sitecore 8.1.

I installed this package: https://www.nuget.org/packages/Krusen.Sitecore.WebApi.Custom/ and I modified the ConfigureWebApi to the following:

public class ConfigureWebApi
{
    public void Process(PipelineArgs args)
    {
        GlobalConfiguration.Configure(config => config.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional }));
        GlobalConfiguration.Configure(config => config.MapHttpAttributeRoutes());
        GlobalConfiguration.Configure(ReplaceControllerSelector);
    }

    private static void ReplaceControllerSelector(HttpConfiguration config)
    {
        config.Services.Replace(typeof (IHttpControllerSelector),
            new CustomHttpControllerSelector(config, new NamespaceQualifiedUniqueNameGenerator()));
    }
}

However, whenever I use post requests, I get the following error:

{"Message":"The requested resource does not support http method 'POST'."}. Get requests work.

This is the implementation of the controller:

[RoutePrefix("api/authentication")]
public class AuthenticationController : ApiController
{
    [Route("email")]
    [HttpPost]
    public bool Login([FromBody] string email)
    {
        return true;
    }
}
Natalie Hedström
  • 2,607
  • 3
  • 25
  • 36
Hykalos
  • 51
  • 2
  • 11
  • I think something must be wrong with your route configuration or you might have used the `HttpPostAttribute` from the MVC namespaces instead of `System.Web.Http`. I guess you are posting to `http://yourdomain.com/api/authentication/email`? – Søren Kruse Jan 11 '16 at 10:48
  • @SørenKruse yes, the URL I test with is as you write, with a random string in the body. I am using the System.Web.Http.HttpPost attribute – Hykalos Jan 11 '16 at 11:23
  • Okay, have you tried without the `API Default` route that you added? – Søren Kruse Jan 11 '16 at 11:27
  • @SørenKruse If I remove the route I get the normal Sitecore error: The requested document was not found, along with the HTML of course – Hykalos Jan 11 '16 at 11:37
  • Then something is wrong. I'm pretty sure it should work out of the box with 8.1. Could you check where `ConfigureWebApi` is placed in the `initialize` pipeline using `showconfig.aspx`? It should be placed right after `Sitecore.Services.Infrastructure.Sitecore.Pipelines.ServicesWebApiInitializer, Sitecore.Services.Infrastructure.Sitecore` – Søren Kruse Jan 11 '16 at 11:54
  • @SørenKruse it is placed where it should be – Hykalos Jan 11 '16 at 12:01
  • @Hykalos I suppose GET is working over a similar url, if yes: try this :) http://stackoverflow.com/questions/3472429/how-do-i-configure-iis-to-accept-post-requests – SilentTremor Jan 11 '16 at 12:24
  • 1
    @SilentTremor sorry, but I don't get what the fix is supposed to be? It shouldn't even accept GET requests due to the `HttpPost` attribute, but it completely ignores it apparantly – Hykalos Jan 11 '16 at 12:29
  • @Hykalos create a GET endpoint under same route and test if you can access it, if yes you your IIS is blocking POST. Example: [Route("email")] [HttpGet] public int GetTest() { return 0; } – SilentTremor Jan 11 '16 at 12:33
  • @SilentTremor If I use the `MapHttpRoute` function, it works using `GET`, however if I only have the `[HttpPost]` function, it uses this function even though it is marked with `HttpPost` i.e. ignoring the tag – Hykalos Jan 11 '16 at 12:49
  • @Hykalos if you are 100% sure you can use GET and POST isn't working on over your web api it means your IIS does not allow to your website to accept POST search on SO for guidelines. – SilentTremor Jan 11 '16 at 12:53
  • @Hykalos I've just tried on a clean install of Sitecore 8.1 rev. 151003. It works without the line you have added in the `Process(..)` method (the part with `API Default`), but with the line you have added I get the same error as you - Method not allowed. – Søren Kruse Jan 11 '16 at 13:43
  • @SørenKruse Just tried reinstalling it and not modify it, however I get the same `The requested document was not found` error... I'm using `Sitecore.NET 8.1 (rev. 151207)` – Hykalos Jan 11 '16 at 14:58
  • @Hykalos I've just tried on a clean install of 8.1 rev. 151207 as well without any issues. Do you have any other routes defined elsewhere that might interfere? – Søren Kruse Jan 11 '16 at 17:16
  • @SørenKruse There shouldn't be. The only implementation there is, is Windsor Castle for the standard MVC (tried disabling it along with your suggestions) and an implementation of an interface. – Hykalos Jan 12 '16 at 08:43
  • @SørenKruse A clean install did the trick. Must have done something funky to mess it up. Thanks for the help – Hykalos Jan 13 '16 at 08:50
  • @Hykalos Glad you got it to work. – Søren Kruse Jan 13 '16 at 08:57
  • @SørenKruse Fun fact: if I name my ApiController `AuthenticationController`, it gives the `Document not found` Sitecore error. Might have been the error all along – Hykalos Jan 13 '16 at 12:09

1 Answers1

1

I figured out what the error was. When my controller was called AuthenticationController it gave the following error:

The requested document was not found

If I called it something else, the web api worked as a charm e.g.

public TestController : ApiController {
    //Code goes here
}
Hykalos
  • 51
  • 2
  • 11