2

我正在玩一些 ServiceStack 演示和示例代码,我试图看看是否有办法消除在 url 中包含 project.serviceclass 名称的需要。我使用了 nuget 包并创建了一个名为 MvcMovieApp 的 ASP.NET MVC 应用程序,并创建了一个名为MovieService.

[Route("/Movie")]
[Route("/Movie/{Name}")]
public class Movie
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Genre { get; set; }
}
public class MovieResponse
{
    public string Result { get; set; }
}

public class MovieService : Service
{
    public object Any(Movie request)
    {
        return new MovieResponse { Result = "The best Movie is " + request.Name };
    }
}

所以要得到回应,我必须要求:

localhost/api/MvcMovieApp.MovieService/Movie/MyMovie

但我希望能够提出请求

本地主机/api/电影/MyMovie

有没有办法做到这一点?

更新

<httpHandlers>
  <add path="api*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
</httpHandlers>
<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>

更新 2:

我能够让它工作。我试图将 ServiceStack 与 MVC 应用程序集成。所以我Application_Start有以下内容:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    // RouteConfig.RegisterRoutes(RouteTable.Routes);
}

注释掉 MVCRouteConfig解决了这个问题,让我可以正确调用 ServiceStack api。这是RegisterRoutes方法。我有这条线,因此 MVC 应该忽略所有进入 API 的内容。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //routes.MapHttpRoute(
    //    name: "DefaultApi",
    //    routeTemplate: "api/{controller}/{id}",
    //    defaults: new { id = RouteParameter.Optional }
    //);

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    routes.IgnoreRoute("api/{*pathInfo}");
    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); //Prevent exceptions for favicon
}

任何想法我做错了什么?谢谢

4

1 回答 1

1

每个配置文件中的 httpHandlers 部分只能出现一次。此外,当它是位置的子项时,您不需要在 httpHandler 中包含 path=api* 。

仅将其用于您的配置:

<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>
于 2012-10-25T13:32:28.957 回答