我正在玩一些ServiceStack演示和示例代码,我试图看看是否有办法消除在url中拥有project.serviceclass名称的需要。我使用了nuget包并创建了一个名为MvcMovieApp的ASP.NET MVC应用程序,并创建了一个名为MovieService
的SS服务。
[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 };
}
}
所以要得到我必须要求的回复:
本地主机/ API / MvcMovieApp.MovieService /电影/ 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);
}
注释掉MVC RouteConfig
解决了问题并让我正确地调用ServiceStack apis。这是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
}
任何想法我做错了什么?感谢
答案 0 :(得分: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>