5

I'm creating utility pages and the structure of the code files is as below:

/Areas/MyTools/Controllers/UpdateField  
/Areas/MyTools/Views/UpdateField/Index.cshtml  

When I browse http://mysite/mytools/updatefield, it works fine and I would like to open the same page for http://mysite/mytools/ as well.

Is it done differently in Sitecore?

SOLUTION:

@Hishaam's answer fixed it. Here is all the code for anyone experiencing the same issue.

MinionsAreaRegistration.cs

This class is created in the project, when you add an area. Add a new route in RegisterArea

public class MinionsAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "MyTools";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "MyTools_default",
                "MyTools/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );

            context.MapRoute(
                "MyTools_Home",
                "MyTools/{id}",
                new { controller = "UpdateField", action = "Index", id = UrlParameter.Optional }                 
            );
        }
    }

RouteConfig.cs

This class is also created when you add a MVC project. Add a new map route in RegisterRoutes.

public static void RegisterRoutes(RouteCollection routes) 
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "MyTools_Home",
                "MyTools/{id}",
                new { controller = "UpdateField", action = "Index", area = "MyTools", id = UrlParameter.Optional }
                );

            routes.MapRoute(
                name: "MyTools_default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
6
  • 1
    Try to add a new Route as follows: routes.MapRoute( "MyTools", // Route name "MyTools/{id}", // URL with parameters new { controller = "UpdateField", action = "Index", area = "MyTools", id = UrlParameter.Optional } // Parameter defaults ); Commented Apr 7, 2018 at 10:01
  • @HishaamNamooya I tried keeping the new route above & also below the default in code. Neither worked :( It still redirects to requested document not found page
    – sukesh
    Commented Apr 7, 2018 at 10:22
  • Did you try the one in my comment? Place it above the default route Commented Apr 7, 2018 at 10:24
  • @HishaamNamooya Yes, tried your code and updated the same in question. Please take a look
    – sukesh
    Commented Apr 7, 2018 at 10:26
  • From the one I've shared, can you please add the namespace also. Example: ` routes.MapRoute( "MyTools", "mytools/{id}", new { controller = "UpdateField", action = "Index", area = "MyTools", id = UrlParameter.Optional }, new[] { "Namespace.Controllers" });` Commented Apr 7, 2018 at 10:29

1 Answer 1

4

You need to register the Area. So, create a registration area under the path /Areas/MyTools/. You can name it MyToolRegistrationArea.cs.

In the MyToolRegistrationArea.cs, add the following

namespace YourNamespace
{
    using System.Web.Mvc;

    public class MyToolRegistrationArea : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "MyTool";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context)
        {
            var myToolRoute = context.MapRoute(
                "MyTool",
                "mytool",
                new { controller = "UpdateField", action = "Index", id = UrlParameter.Optional },
                new[] { "YourNamespace.Controllers" });

            myToolRoute.RouteHandler = new Sitecore.Mvc.Routing.RouteHandlerWrapper(myToolRoute.RouteHandler);
        }
    }
}

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.