13

We can an MVC app that uses the default folder conventions for the HTML views, but we'd like to set up alternate "Services" folder with controllers used only for web services returning xml or json.

So the route "/Services/Tasks/List" would be routed to "/Services/TaskService.cs", while "/Tasks/List" would be routed to the standard "/Controllers/TaskController.cs"

We'd like to keep the service controllers separate from the view controllers. We don't think areas or using another project will work. What would be the best way to approach this?

Sterling Nichols
  • 1,518
  • 2
  • 18
  • 22

4 Answers4

14

You can do this using Routing, and keeping the controllers in separate namespaces. MapRoute lets you specify which namespace corresponds to a route.

Example

Given this controllers

namespace CustomControllerFactory.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
           return new ContentResult("Controllers");
        }
    }
}

namespace CustomControllerFactory.ServiceControllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
           return new ContentResult("ServiceControllers");
        }
    }
}

And the following routing

 routes.MapRoute(
           "Services",
           "Services/{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "CustomControllerFactory.ServiceControllers" } // Namespace
        );


        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            new string[] { "CustomControllerFactory.Controllers"} // Namespace
        );

You should expect the following responses

/Services/Home

ServiceController

/Home

Controllers

CGK
  • 2,662
  • 21
  • 24
  • You can do this and return a `ContentResult` or any non-view result, just remember if you decide to return any sort of view, you will need to create a custom view engine. – Ahmad Dec 07 '10 at 05:24
  • 1
    Yes, the answer above only solves the routing part of the problem. If you return a ViewResult, you'll need to modify conventions for choosing the view. If you choose to implement a custom view engine,[this](http://stackoverflow.com/questions/799838/asp-net-mvc-how-to-specify-which-folder-the-view-pages-reside-in/809676#809676) might help. You can also try basing your service controllers on a base class which overrides View(), and implement your conventions there. It would be nice to know which path you take. – CGK Dec 07 '10 at 14:15
2

In Asp.Net Core: I used AreaFeature library ServiceCollectionExtensions

First set your middleware;

    services.AddMvc(options => 
        options.EnableEndpointRouting = false).
        AddFeatureFolders().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Latest);

In the extenssion:

return AddFeatureFolders(services, new FeatureFolderOptions());

implement as according your requirement:

 public static IMvcBuilder AddFeatureFolders(this IMvcBuilder services, FeatureFolderOptions options)
        {
            if (services == null)
                throw new ArgumentNullException(nameof(services));

            if (options == null)
                throw new ArgumentException(nameof(options));

            var expander = new FeatureViewLocationExpander(options);

            services.AddMvcOptions(o => o.Conventions.Add(new FeatureControllerModelConvention(options)))
                .AddRazorOptions(o =>
                {
                    o.ViewLocationFormats.Clear();
                    o.ViewLocationFormats.Add(options.FeatureNamePlaceholder + @"\{0}.cshtml");
                    o.ViewLocationFormats.Add(options.FeatureNamePlaceholder + @"\_Views\{0}.cshtml");
                    o.ViewLocationFormats.Add(options.FeatureFolderName + @"\Shared\{0}.cshtml");
                    o.ViewLocationFormats.Add(options.DefaultViewLocation);

                    o.ViewLocationExpanders.Add(expander);
                });

            return services;
        }

In my case i preferred a separated _View folder for views in the features folders like that

~/Features/Account/_Views/login.cshtml
~/Features/Account/AccountController.cs
~/Features/Account/AccountModel.cs
Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
0

If you see yellow folder names Add folder Name in root

After, you have to modify routes.MapRoute into "App_Start > RouteConfig"

Modify Default route

routes.MapRoute(
          "Default",
          "{controller}/{action}/{id}",
          new { controller = "Home", action = "Index", id =     UrlParameter.Optional },
          new string[] { "mvcPartialView.HomeController" } // Namespace 
      );

and Add this

routes.MapRoute(
       "ApiControllerOne", // Name of folder
       "ApiControllerOne/{controller}/{action}/{id}",
        new { controller = "ApiFactory", action = "callFactoryOne", id = UrlParameter.Optional },
        new string[] { "mvcPartialView.ApiControllerOne" } // Namespace
    );
0

You'll want to create your own controller factory implementing IControllerFactory.

Check out http://nayyeri.net/custom-controller-factory-in-asp-net-mvc for an example.

Michael Shimmins
  • 19,961
  • 7
  • 57
  • 90