Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServiceStack and MVC4 not wiring up

I have an existing MVC 4 application. I wanted to add Service Stack to it. I tried installing the MVC host nuget package:

Install-Package ServiceStack.Host.Mvc

It installed 2 files in App_Start. I noticed I had to make a slight change because I was getting a build error:

In App_State/WebServiceExamples.cs, I had to update the interface references:

From: public class HelloService : Service

To: public class HelloService : ServiceStack.ServiceInterface.Service

I then went ahead and double checked the Web.config settings:

<location path="api">
  <system.web>
    <httpHandlers>
      <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
    </httpHandlers>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
  <!-- Required for IIS 7.0 -->
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <add path="*" name="ServiceStack.Factory" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
    </handlers>
  </system.webServer>
</location>

I then ran the application and went to /api, I got a 404. From some further research I decided to manually update the endpoint via the apphost file:

SetConfig(new EndpointHostConfig
{
ServiceStackHandlerFactoryPath = "api",
});

This also didn't seem to work. What else am I missing?

Thanks for your time.

like image 553
TheWebGuy Avatar asked Mar 08 '13 20:03

TheWebGuy


1 Answers

This should be in your web.config file as well:

<httpHandlers>
  <add path="api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>

Also don't forget to remove the MVC '/api' route/path. You need to remove it so that ServiceStack and MVC don't compete for the '/api' route/path.

//REMOVE THIS FROM RouteConfig
routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
);

If you want to be real explicit you can also add this to the RouteConfig

routes.IgnoreRoute ("api/{*pathInfo}");
like image 161
paaschpa Avatar answered Sep 28 '22 09:09

paaschpa