1

Controller:

public class ExampleController : Controller
{
    private readonly ISitecoreContext _sitecoreContext;

    public ExampleController ()
    {
        
    }     

    [System.Web.Http.HttpGet]
    // GET: Manifest
    public JsonResult Test()
    {
        return Json("test");         
    }
}

Register Route Pipeline:

public class RegisterTestApiRoute
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapHttpRoute(
            "testApi",
            "api/testing/{controller}/{action}"
        );
    }
}

Patching route in config:

<pipelines>
  <initialize>
    <processor type="TestProject.Pipelines.RegisterTestApiRoute, TestProject" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
  </initialize>
</pipelines>

With all above in place the following default Sitecore webapi route works fine:

http://localsitecore/api/sitecore/example/test

but my custom route

http://localsitecore/api/testing/example/test

returns below error:

{"Message":"No HTTP resource was found that matches the request URI 'http://localsitecore/api/testing/example/test'.","MessageDetail":"No type was found that matches the controller named 'example'."}

I'm not sure why controller is not found, in a properly compiled code, I've tried below:

  • Restarted IIS
  • killed worker processes
  • cleared Visual Studio temp files

No luck..

even browsed my project dll with reflector, the controller exists..

Am I missing anything here...?

6
  • Can you change MapHttpRoute to MapRoute? We are using this without any problem Commented Apr 20, 2018 at 6:09
  • Second problem is that you are using Sitecore webapi path /api/testing. We are usually using clientapi/.... instead of api/ not to clash with Sitecore OOTB controllers. try also to change /api/ to something else e.g. /nareshapi/ for now :) Commented Apr 20, 2018 at 6:13
  • Also check in Showconfig.aspx whether your RegisterTestApiRoute processor type is present in configuration Commented Apr 20, 2018 at 6:14
  • changing MapHttpRoute to MapRoute, worked... Looks like MapHttpRoute is meant for ApiController type and MapRoute for Controller type... Thank You, @Peter Prochazka.
    – nari447
    Commented Apr 20, 2018 at 6:28
  • please add this as answer for me to accept... :)
    – nari447
    Commented Apr 20, 2018 at 6:30

1 Answer 1

3

Your route pipeline definition should be changed to this one:

public class RegisterTestApiRoute
{
    public virtual void Process(PipelineArgs args)
    {
        RouteTable.Routes.MapRoute(
            "testApi",
            "api/testing/{controller}/{action}"
        );
    }
}

as MapRoute is meant for "normal" ASP.NET MVC controllers whereas MapHttpRoute is meant for Web API controllers.

More details about this topic can be found here: https://stackoverflow.com/questions/12042853/difference-between-maphttproute-and-maproute

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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