2

I am getting 404s (requested document not found) when I try to use a custom route I created per the guide here: https://kb.sitecore.net/articles/700677. My patch config is showing up in showconfig, here's what it looks like:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="[obfuscated].Pipelines.Routes.RegisterCustomRoute, [obfuscated]" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>

The processor code:

namespace BrightHorizons.Pipelines.Routes

{
    public class RegisterCustomRoute
    {
        public virtual void Process(PipelineArgs args)
        {
            RouteTable.Routes.MapRoute("CustomRoute", "api/{controller}/{action}/{id}");
        }
    }
}

The controller action and controller name seem correct... here's what that looks like:

public class FindACenterController : GlassController
{
    public ActionResult FindACenterMap()
    {
        ...
    }
}

I am trying to access it like this: http://mysite/api/FindACenter/FindACenterMap

I also tried using a basic Default route in the RouteConfig.cs (/{controller}/{action}/{id}) under App_Start before that and it did not work either. I am really not good at MVC routing so looking for some guidance.

UPDATE: FindACenterMap is now working, however this is not working for a different controller. Added a new route:

RouteTable.Routes.MapRoute("SubmitScheduleVisit",
    "api/SubmitScheduleVisit",
    new { controller = "FindACenter", action = "SubmitScheduleVisit" },
    new { httpMethod = new HttpMethodConstraint("POST") }
);

It's in the same controller. This is the action definition:

[HttpPost]
public ActionResult SubmitScheduleVisit(string selectedDate, string selectedTime, string firstName, string lastName,
            string emailAddress, string phoneNumber, string whenToStart, List<string> childDatesOfBirth)
{
     ....
}

SOLUTION TO UPDATE (Not a solution to the original question): I simply had to run the query in Postman. I was testing the API call in Firefox which meant it was getting run as a GET, not a POST. Solved.

2
  • Apparently there is this thing called Global.asax that I need to run the RegisterRoutes()? I thought that was web forms only? I added that and call RegisterRoutes() and no difference
    – Paul
    Oct 9, 2018 at 2:23
  • You do not need to modify the global.asax. Using the initialize pipeline replaces that change.
    – Chris Auer
    Oct 9, 2018 at 16:01

1 Answer 1

2

The code below is code I reuse constantly. The only caveat is that is the controller is in a different project/library than the route initialization code, strange thing happen. I always keep the controller and initialize code together.

Your url would now be http://mysite/api/FindACenterMap

Please see this answer while modification of the global.asax is not required. Global.asax Application_Start not hit after upgrade to Sitecore 8.2

The route

public virtual void Process(PipelineArgs args)
{
    RouteTable.Routes.MapRoute("FindACenterMap",
        "api/FindACenterMap",
        new { controller = "FindACenter", action = "FindACenterMap" },
        new { httpMethod = new HttpMethodConstraint("GET") }
    );

    RouteTable.Routes.MapRoute("SubmitScheduleVisit",
        "api/SubmitScheduleVisit",
        new { controller = "FindACenter", action = "SubmitScheduleVisit" },
        new { httpMethod = new HttpMethodConstraint("POST") }
    );
}

The controller

public class FindACenterController : GlassController
{
    [HttpGet]
    public ActionResult FindACenterMap()
    {
        ...
    }

    [HttpPost]
    public ActionResult SubmitScheduleVisit(string selectedDate, string selectedTime, string firstName, string lastName, string emailAddress, string phoneNumber, string whenToStart, List<string> childDatesOfBirth)
    {
            ....
    }
 }

The config

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <initialize>
        <processor type="[obfuscated].Pipelines.Routes.RegisterCustomRoute, [obfuscated]" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
      </initialize>
    </pipelines>
  </sitecore>
</configuration>
6
  • Hmm, I am getting a yellow screen error here - The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character. Parameter name: routeUrl, and it's pointing to the route in RegisterCustomRoute.cs. If I remove the leading / I get an IIS detailed 404 again.
    – Paul
    Oct 9, 2018 at 18:07
  • Actually it is working for FindACenterMap. However when I use this for another controller action which is a POST (and I change the http method to POST) I am getting the IIS detailed 404. I have [HttpPost] above the controller definition.
    – Paul
    Oct 9, 2018 at 18:16
  • Did you change the route to POST? like this, new { httpMethod = new HttpMethodConstraint("POST") } with [HttpPost] as the attribute? And change the route name to something else like RouteTable.Routes.MapRoute("FindACenterMap-post",
    – Chris Auer
    Oct 9, 2018 at 18:18
  • Yep, I tried both of those, still a 404. I added more details to the question.
    – Paul
    Oct 9, 2018 at 18:24
  • Also, if I change the controller action and the route to be a GET instead of a POST, it works. Wonder why it doesn't like POST.
    – Paul
    Oct 9, 2018 at 18:31

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.