与MVC并行运行ServiceStack

时间:2013-04-19 17:48:08

标签: asp.net-mvc-4 servicestack

我设法与MVC4一起运行ServiceStack,但我仍有一点问题,希望有人可以帮助我。

通过VS2012执行调试会话时,everthying工作正常 - 浏览器打开,第一页加载良好。但是当刷新页面并尝试转到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包装任何通过浏览器(所有控制器)的调用以及对/ 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以包装整个服务器时出错,请指出他们。

1 个答案:

答案 0 :(得分:1)

根据您的尝试,我建议从一个普通的ASP.NET Web应用程序开始。设置好项目后,可以使用NuGet

Install-Package ServiceStack.Host.AspNet 

或者只需按照配置here进行操作即可。这样做会在根路径/.

上运行ServiceStack

在您的设置中,您不需要这一行......

SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "api", DefaultContentType = ContentType.Json });

添加它告诉ServiceStack仅处理包含'/api'路径的请求,而该路径不是您想要的。