Sitecore - adding a route to Application_Start

I am using sitecore 7.5 and I need to add a new route in application_start in order to use it in the ajax call, but when I run the application it seems that sitecore is dealing with the route as a content item please

+3


source to share


1 answer


Here is the code that creates the route for you. In global.asax.cs, you will call RegisterRoutes from your App_Start event handler :

    protected void Application_Start()
    {
        RouteConfig.RegisterRoutes(RouteTable.Routes);
    }

      

And there you will specify your route as:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
             name: "test",
             url: "mvc/Forms/{action}/{id}",
             defaults: new { controller = "Forms", action = "Test", id = UrlParameter.Optional }
           );
    }

      

In this case, you will have / mvc / prefix which will process your route before specifying the controller, so you will call it like:

/mvc/Forms/Test/{you_may_pass_some_optional_GUID_here}

      

This will result in the action method of the FormsController Test class (row id), but you can omit the id parameter



A little attention: Note that setting the route to Application_Start is not the best way to do this; it is much better to implement mapping routes in the Initialize pipeline as it follows Sitecore's architecture:

public class Initialize
{
    public void Process(PipelineArgs args)
    {
        MapRoutes();
    }

    private void MapRoutes()
    {
        RouteTable.Routes.MapRoute(
                "Forms.Test", 
                "forms/test", 
                new
                {
                    controller = "FormsController",
                    action = "Test"
                },
                new[] { "Forms.Controller.Namespace" });
     }
}

      

Rest of implementation: Also I previously wrote an article on my blog about how to implement an ajax call for a route that will guide you through the rest of the implementation process:

http://blog.martinmiles.net/post/editing-content-on-a-cd-server

Update: Also make sure you have a handler in your config to handle your prefix, see below:

<customHandlers>
    <handler trigger="~/mvc/" handler="sitecore_mvc.ashx" />

      

+4


source







All Articles