GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Mvc integration Mvc integration
Demis Bellot edited this page
This page has moved to docs.servicestack.net/mvc-integrationYou can easily add ServiceStack to any ASP.NET MVC project by getting it from NuGet with: 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. Typically when hosting ServiceStack with MVC you'd want to host it at the <location path="api"> <system.web> <httpHandlers> <add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*"/> </httpHandlers> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <validation validateIntegratedModeConfiguration="false" /> <handlers> <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" /> </handlers> </system.webServer> </location>
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" }); ControllerBuilder.Current.SetControllerFactory( new FunqControllerFactory(container)); } } [Route("/hello/{Name}")] public class Hello { public string Name { get; set; } } public class MyServices : Service { public object Any(Hello request) { return request; } } Whilst ServiceStack automatically tries to infer the handler path based on the SetConfig(new HostConfig { HandlerFactoryPath = "api", }); 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 public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); } } You MUST also register ServiceStacks routes.IgnoreRoute("api/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); Place them before the current entries the method. For MVC applications that include WebApi, you would need to unregister it by commenting out this line: //WebApiConfig.Register(GlobalConfiguration.Configuration);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 void Configure(Funq.Container container) { //Set MVC to use the same Funq IOC as ServiceStack ControllerBuilder.Current.SetControllerFactory( new FunqControllerFactory(container)); } Just like in ServiceStack, you can retrieve an auto-wired Service and execute it using public HelloController : ServiceStackController { public void Index(string name) { using (var hello = HostContext. ResolveService<HelloService>(base.HttpContext)) { 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. 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) ServiceStack.MiniProfiler.Profiler.Start(); } protected void Application_EndRequest(object src, EventArgs e) { ServiceStack.MiniProfiler.Profiler.Stop(); } For more info on the MiniProfiler see the Built in profiling wiki. The Urls for metadata page and included Services:
Once you have MVC + ServiceStack up and running checkout ServiceStack Integration docs to explore different ways of accessing ServiceStack from MVC.
Clone this wiki locally |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |