Sitecore - adding a route to Application_Start
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" />
source to share