1

I have a pre-existing Sitecore 9 instance that houses an existing website.

I have created a new solution, with a new web project, and am attempting to install this project into the existing Sitecore 9 instance. This new web project simply exposes a handful of Controller Actions that return JSON.

I have decided to create this new project using MVC Areas. Currently, the project is empty except for an Area Registration and a single controller with a single action.

public class MyApiAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "MyApi";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.Namespaces.Add("MyApi.Web.Areas.MyApi.Controllers");

        var aboutRoute = context.MapRoute(
            "MyApi_about",
            "myapi/about/version",
            new { controller = "About", action = "Version"},
            new[] {"MyApi.Web.Areas.MyApi.Controllers"});

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

This project has the following nuget references:

<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net462" />
<package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net462" />
<package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net462" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net462" />
<package id="Sitecore.Mvc.NoReferences" version="9.0.171219" targetFramework="net462" developmentDependency="true" />

And the AboutController referenced above simply returns a JSON Response with the assembly info version number.

When I compile this project, I get a single .dll file. I copy this .dll file to my running Sitecore instance and restart it within IIS. When I go to check my logs, however, I do not see where the "MyApi" area is registered. What else must be done to register a custom area within Sitecore?

1 Answer 1

3

I am on Sitecore 9.1. I've added the following Area Registration and Controller and it seems working:

MyApiAreaRegistration:

using System.Web.Mvc;

namespace Test.Areas.MyApi
{
    public class MyApiAreaRegistration : AreaRegistration 
    {
        public override string AreaName 
        {
            get 
            {
                return "MyApi";
            }
        }

        public override void RegisterArea(AreaRegistrationContext context) 
        {
            context.Namespaces.Add("Test.Areas.MyApi.Controllers");

            context.MapRoute(
                "MyApi_default",
                    "myapi/about/version",
                    new { controller = "About", action = "Version" },
                    new[] { "Test.Areas.MyApi.Controllers" }
            );
        }
    }
}

Controller:

using System.Web.Mvc;

namespace Test.Areas.MyApi.Controllers
{
    public class AboutController : Sitecore.Mvc.Controllers.SitecoreController
    {
        [HttpGet]
        public ActionResult Version()
        {
            return Content("test", "application/json");
        }
    }
}

Solution structure:

asp.net area in Sitecore

And, finally, result:

area controller execution result

Please, compare it with your implementation.

Your Answer

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

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