我设法将 ServiceStack 与 MVC4 并排运行,但我仍然有一个小问题,希望有人能帮助我解决这个问题。
当通过 VS2012 执行调试 session 时,一切正常 - 浏览器打开并且第一页加载良好。但是当刷新页面并尝试访问 http://localhost:62322/Content/site.css
时,会显示以下错误:
Handler for Request not found:
Request.ApplicationPath: /
Request.CurrentExecutionFilePath: /Content/site.css
Request.FilePath: /Content/site.css
Request.HttpMethod: GET
Request.MapPath('~'): D:\All\Projects\ExampleProject\trunk\ExampleProject\ExampleProject\
Request.Path: /Content/site.css
Request.PathInfo:
Request.ResolvedPathInfo: /Content/site.css
Request.PhysicalPath: D:\All\Projects\ExampleProject\trunk\ExampleProject\ExampleProject\Content\site.css
Request.PhysicalApplicationPath: D:\All\Projects\ExampleProject\trunk\ExampleProject\ExampleProject\
Request.QueryString:
Request.RawUrl: /Content/site.css
Request.Url.AbsoluteUri: http://localhost:62322/Content/site.css
Request.Url.AbsolutePath: /Content/site.css
Request.Url.Fragment:
Request.Url.Host: localhost
Request.Url.LocalPath: /Content/site.css
Request.Url.Port: 62322
Request.Url.Query:
Request.Url.Scheme: http
Request.Url.Segments: System.String[]
App.IsIntegratedPipeline: False
App.WebHostPhysicalPath: D:\All\Projects\ExampleProject\trunk\ExampleProject\ExampleProject
App.DefaultHandler: DefaultHttpHandler
App.DebugLastHandlerArgs: GET|/Content/site.css|D:\All\Projects\ExampleProject\trunk\ExampleProject\ExampleProject\Content\site.css
但是,如果我删除 AppHost.cs 中的以下代码行,一切正常,并且总能找到 site.css 的处理程序:
SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api", DefaultContentType = ContentType.Json });
我对项目的要求是用 ServiceStack 包装通过浏览器(所有 Controller )的任何调用以及对/api 的调用,这应该由 ServiceStack 服务处理。 我按照这里的说明操作: https://github.com/ServiceStack/ServiceStack/wiki/Run-servicestack-side-by-side-with-another-web-framework 我的 web.config 看起来像这样:
<system.web>
<!--...-->
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
<!--...-->
</system.web>
<location path="api">
<system.web>
<httpHandlers>
<add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
</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>
有谁知道为什么 site.css 的处理程序有时找到有时找不到? 此外,如果我在配置 ServiceStack 来包装我的整个服务器时犯了错误,请指出它们。
最佳答案
根据您的尝试,我建议您从普通的 ASP.NET Web 应用程序开始。设置项目后,您可以使用 NuGet
Install-Package ServiceStack.Host.AspNet
或者直接按照配置here .这样做会在根路径/运行 ServiceStack。
在你上面的设置中你不需要这一行...
SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api", DefaultContentType = ContentType.Json });
添加它是告诉 ServiceStack 只处理包含 '/api'
路径的请求,这不是您想要的。
关于asp.net-mvc-4 - 与 MVC 并排运行 ServiceStack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16110598/