8

Sitecore 8.2 rev 160729

My understanding is that, as of Sitecore 8.2, you no longer need to call .MapHttpAttributeRoutes() because it's now included (in fact, it'll throw an error if you do). However, Sitecore is not picking up my ApiControllers using attribute based routing.

public class SampleController : ApiController
{
    [Route("-/api/sample/test"), HttpGet]
    public string Test()
    {
        return "Hello World!";
    }
}

The above route does not appear in the route tables, and the URL gives me a 404.

Do I need to tell Sitecore which assemblies to look for routes in, or is there an additional configuration step I'm missing?

  • I deleted my solution because your OP explicitly mentioned 8.2 and if you're getting the same result with and without the dash then the solution I posted was likely for an 8.0.x specific bug. – Zachary Kniebel Jan 25 '17 at 22:03
  • One thing I just noticed is that your action is set to return a string instead of a ContentResult. Unless there is something special in Sitecore that allows controller actions to have a value type as the return type of an action, then this may be your culprit. I would expect that you would see "No action method found" errors, however, so I don't think this is your only issue – Zachary Kniebel Jan 25 '17 at 22:09
  • This is a Web API controller, not an MVC controller, so I think I'm fine returning a string. I believe ContentResult is only a thing for MVC Controllers. – Ben Lipson Jan 25 '17 at 22:18
  • 1
    You're 100% correct. End of the day for me - I must be losing it. – Zachary Kniebel Jan 25 '17 at 22:23
  • 1
    I installed fresh Sc 8.2.160729 via SIM (using default settings), created a simple class library with the SampleController, copied it to the website\bin and successfully received the "Hello world!" response. Are there any customizations on your side or any errors in logs? – Serhii Shushliapin Jan 27 '17 at 9:27
2

I think since you have a DI configured that will be resetting any routes registered after you've added them so you need to do this afterwards in the initialize Pipeline like so:

Create a pipeline to register your route:

public class RegisterMySampleRoute
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapHttpRoute("my_sample_route", 
            "mysample/api/{controller}/{action}", 
            new {controller = "SampleController"}
        );
    }
}

Then patch it in like so:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <initialize>
                <processor type="MyNamespace.Pipelines.RegisterMySampleRoute, MyNamespace" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
            </initialize>
        </pipelines>
    </sitecore>
</configuration>

That should do it.

  • I follow the same process but the new route never matched and i always get invalid url error whenever i try hitting this url from browser. – Arvind Gehlot Jan 30 '18 at 12:30

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.