我有一个现有的MVC 4应用程序。我想为它添加服务堆栈。我尝试安装MVC主机nuget包:
Install-Package ServiceStack.Host.Mvc
它在App_Start中安装了2个文件。我注意到我必须做一些小改动,因为我遇到了构建错误:
在App_State / WebServiceExamples.cs中,我不得不更新接口引用:
来自:public class HelloService : Service
收件人:public class HelloService : ServiceStack.ServiceInterface.Service
然后我继续并仔细检查了Web.config设置:
<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>
然后我运行了应用程序并转到/ api,我得到了404.从进一步的研究中,我决定通过apphost文件手动更新端点:
SetConfig(new EndpointHostConfig
{
ServiceStackHandlerFactoryPath = "api",
});
这似乎也不起作用。我还缺少什么?
感谢您的时间。
答案 0 :(得分:6)
这也应该在你的web.config文件中:
<httpHandlers>
<add path="api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
另外,不要忘记删除 MVC'/ api'路由/路径。您需要删除它,以便ServiceStack和MVC不竞争'/ api'路由/路径。
//REMOVE THIS FROM RouteConfig
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
);
如果您想要真正明确,您也可以将其添加到RouteConfig
routes.IgnoreRoute ("api/{*pathInfo}");