1

I have created a WebApi controller, and it's returning the payload OK (i.e. I see the correct response in the browser), however the status of the request is a 404, which obviously causes problems with JS clients.

Why is the status 404 when clearly the route exists?

I have even tried setting the response status explicitly:

return Request.CreateResponse(HttpStatusCode.OK, "hello");

But still get a 404?

I'm using Sitecore version 8.2 update 4.

Controller:

public class SearchApiController 
    : ApiController
{

    [HttpGet]
    public HttpResponseMessage Search()
    {
        var searchResults = GetSearchResults();

        return Request.CreateResponse(HttpStatusCode.OK, searchResults);
    }
}

Route mapping via pipeline:

public class InitializeRoutes
{
    public void Process(PipelineArgs args)
    {
        var config = GlobalConfiguration.Configuration;

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

Config:

    <pipelines>
        <initialize>
            <processor type="Feature.Search.Infrastructure.Pipelines.InitializeRoutes, Feature.Search" resolve="true" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeGlobalFilters, Sitecore.Mvc']" />
        </initialize>
    </pipelines>
  • You need to terminate the request, to stop further processing on the request. That would be my guess anyway, otherwise you need to share a bit more on how you've set this up. – Mark Cassidy Nov 9 '17 at 14:02
  • @MarkCassidy updated with extra details – David Masters Nov 9 '17 at 14:12
  • @MarkCassidy when you say "Terminate the request" - how would that be done? – David Masters Nov 9 '17 at 14:13
  • What is the URL you are using to access the API? – Ahmed Okour Nov 9 '17 at 14:41
  • 1
    @AhmedOkour that's it. A team member has added a processor that is assuming 404 status when there is not Sitecore item in context. Thanks – David Masters Nov 9 '17 at 15:54
2

It turned out that another member of the team had added a custom pipeline to handle 404's. It's code was checking the Sitecore.Context.Item and returning a 404 status if null, which obviously it would be when invoking the api url.

1

Change your code. The HttpResponseMessage is deprecated and is replaced by IHttpActionResult. While there are probably better and more Sitecore-esque ways to achieve what you are doing here, a straight up modification of your code would look like this:

public class SearchApiController 
    : ApiController
{

    [HttpGet]
    public IHttpActionResult Search()
    {
        var searchResults = GetSearchResults();
        if(searchResults == null)
            return NotFound();
        return Ok(searchResults);
    }
}

Reference: Web API Best Approach for returning HttpResponseMessage

Recommended followup reading: Implementing a WebApi service using ServicesApiController in Sitecore 8

  • I was using IHttpActionResult originally but thought I'd try with the response message just to see if setting the status explicitly would make a difference. Is that article still applicable to 8.2? – David Masters Nov 9 '17 at 15:40
  • 1
    I would assume so. Further, the article actually describes a better approach for Sitecore APIs - I just didn't want to jump out with a "you're all wrong" approach ;-) I believe what is happening for you is, your API call executes but Sitecore continues to process the request - which then ultimately 404s. And that lies in the fact, that the request is not recognized as a full "API request" rather a "page request". – Mark Cassidy Nov 9 '17 at 15:57
1

You can try Web API attribute routing which in Sitecore 8.2 is supported with no conflicts with ​Sitecore.Services.Client.

You don't even need to configure Web API to use attribute routing configurations, all you need to add is to add the route prefixes to your you controllers/Action like this:

[RoutePrefix("api/searchapi")]
public class SearchApiController : ApiController
{
    [HttpGet]
    [Route("search")]
    public HttpResponseMessage Search()
    {
        var searchResults = GetSearchResults();

        return  Ok(searchResults);
    }
}
  • This doesn't work for me. – David Masters Nov 9 '17 at 15:17
  • @DavidMasters This could be obvious, but worth checking if the dll that has your controller is deployed to your bin, I didn't have any problem with the code above – Ahmed Okour Nov 9 '17 at 15:19
  • @DavidMasters Also, Do you have any other custom route mapping? – Ahmed Okour Nov 9 '17 at 15:20
  • It's definitely there. Just to re-iterate: when I type the url into the browser, it works - I can see the data. But in the network tab I can see the status is a 404. – David Masters Nov 9 '17 at 15:21
0

Can you please check http://{HostName}/sitecore/admin/showconfig.aspx and verify your pipeline processor position?

I would suggest to move it to the last position by applying Sitecore patch configuration or move it to zzz config folder.

We are having the same setup(Sitecore 8.2 update-4) except the case I mentioned above and everything works fine.

-1

you can used SitecoreController like this and

then use api/{controller}/{action}/{id} or api/SearchApi/Search

public class SearchApiController 
    : SitecoreController 
{

    [HttpGet]
    public IHttpActionResult Search()
    {
        var searchResults = GetSearchResults();
        if(searchResults == null)
            return NotFound();
        return Ok(searchResults);
    }
}
  • Its work find for me.Please check & apply. – Agent47 Jun 26 '18 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.