Edit on GitHub

MVC Integration

Add ServiceStack to an existing MVC Project

You can easily add ServiceStack to any ASP.NET MVC project by getting it from NuGet with:

PM> Install-Package ServiceStack.Mvc

This install ServiceStack with additional (and optional) integration support for MVC letting you use ServiceStack’s IOC to initialize MVC controllers or create MVC Controllers with built-in access to ServiceStack’s components.

Enabling ServiceStack in Web.Config

Typically when hosting ServiceStack with MVC you’d want to host it at the /api custom route which you can do by adding the IIS7+ configuration below to your Web.config:

<location path="api">
      <add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" 

    <modules runAllManagedModulesForAllRequests="true"/>
    <validation validateIntegratedModeConfiguration="false" />
      <add path="*" name="ServiceStack.Factory" 
           type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" 
           resourceType="Unspecified" allowPathInfo="true" />

See Run Side-by-Side with another web framework for other web.config examples of hosting ServiceStack, e.g with IIS6/Mono.

Add your ServiceStack AppHost

The smallest ServiceStack AppHost for MVC would look something like:

public class AppHost : AppHostBase
    public AppHost() : base("MVC 4", typeof(MyServices).Assembly) {}

    public override void Configure(Container container)
        SetConfig(new HostConfig { 
            HandlerFactoryPath = "api" 

            new FunqControllerFactory(container));

public class Hello
    public string Name { get; set; }

public class MyServices : Service
    public object Any(Hello request)
        return request;

Inferring ServiceStack’s HandlerFactoryPath (/api)

Whilst ServiceStack automatically tries to infer the handler path based on the <location> tag, if there’s an uncommon Web.config setup or there are some other issue inferring it, it’ recommended to also explicitly set the /api handler path in Config.HandlerFactoryPath, e.g:

SetConfig(new HostConfig { 
    HandlerFactoryPath = "api",

Initializing ServiceStack

Hosting in ASP.NET MVC is very similar to hosting in any ASP.NET framework, i.e. The ServiceStack AppHost still needs to be initialized on start up in your Global.asax.cs (or WebActivator), e.g:

public class Global : System.Web.HttpApplication
    protected void Application_Start(object sender, EventArgs e)
        new AppHost().Init();

You MUST also register ServiceStacks /api path by adding the lines below to MvcApplication.RegisterRoutes(RouteCollection) in the Global.asax:

routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); 

Place them before the current entries the method.

Removing Web API

For MVC applications that include WebApi, you would need to unregister it by commenting out this line:


Optional Configuration

Sharing dependencies with MVC Controllers

To register all your dependencies in your ServiceStack AppHost, register an MVC Controller factory so both your MVC Controllers and ServiceStack services get auto-wired with these dependencies in your AppHost.Configure(), e.g:

void Configure(Funq.Container container) 
    //Set MVC to use the same Funq IOC as ServiceStack
        new FunqControllerFactory(container));

Calling ServiceStack Services from MVC Controllers

Just like in ServiceStack, you can retrieve an auto-wired Service and execute it using HostContext.ResolveService<TService>(), e.g:

public HelloController : ServiceStackController 
    public void Index(string name) 
        using (var hello = HostContext.
           ViewBag.GreetResult = hello.Get(name).Result;
           return View();

Another cleaner way to share functionality between MVC and ServiceStack is to get them both injected with a shared dependency. See the IGreeter example on StackOverflow.

Adding Mini Profiler

To enable the Mini Profiler add the following lines in to MvcApplication in Global.asax.cs:

protected void Application_BeginRequest(object src, EventArgs e)
    if (Request.IsLocal)

protected void Application_EndRequest(object src, EventArgs e)

For more info on the MiniProfiler see the Built in profiling docs.

The Urls for metadata page and included Services:

  • /api/metadata - Auto generated metadata pages
  • /api/hello - Simple Hello World Service
  • /api/todos - Simple REST Service see: http://todos.servicestack.net
  • /default.htm - Backbone.js TODO application talking to the TODO REST service at /api/todos

Accessing ServiceStack from MVC

Once you have MVC + ServiceStack up and running checkout ServiceStack Integration docs to explore different ways of accessing ServiceStack from MVC.