简体   繁体   中英

How do I install service stack 4 side-by-side with asp.net MVC 4?

I've gone through all of the steps in the servicestack documentation and followed the advice of a similar post here on Stack Overflow, but whenever I try to access my "/api/" route, I get the following error:

Handler for Request not found: 


Request.HttpMethod: GET
Request.PathInfo: /api/metadata
Request.QueryString: 
Request.RawUrl: /api/api/metadata

Relevant sections of code:

AppHost:

public class HolidayEventPlannerAppHost : AppHostBase
    {
        public HolidayEventPlannerAppHost() : base("Holiday Event Planner App Host", typeof(HelloService).Assembly) { }

        public override void Configure(Funq.Container container)
        {
            SetConfig(new HostConfig { HandlerFactoryPath = "api" });
        }
    }

Routeconfig.cs:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

In addition, when I type "localhost:port/api" in my browser's address bar, the browser gets redirected to "localhost:port/api/api/metadata" which is where I get the error message. It won't let me access "localhost:port/api/metadata".

Finally someone that help to eliminate the metadata completely. You need to put:

SetConfig(new HostConfig
    {
        HandlerFactoryPath = "api",
        MetadataRedirectPath = "/"
    }); 

I am using ServiceStack 3.9.70, for me in order to use ServiceStack along with MVC WebApi I had to do this:

SetConfig(new EndpointHostConfig
    {
        ServiceStackHandlerFactoryPath = "sapi",
        MetadataRedirectPath = "sapi/metadata"
     });

This way I can use api for WebAPI and sapi for ServiceStack. Don't forget you need to modify the Web.Config also, full instruction here .

There's an existing template for hosting ServiceStack with MVC4 , you'd also want to ensure that your Web.config is properly configured for ServiceStack, which you can compare with the Mvc4 Web.Config .

More information about integration with MVC is available on the wiki .

I've had similar issues with this redirect path since using v4 of ServiceStack.

As a fix, I found that overriding the MetadataRedirectPath with the following HostConfig worked:

        SetConfig(new HostConfig
        {
            HandlerFactoryPath = "api",
            MetadataRedirectPath = "/metadata"
        });

I think without this config, a bug is being exposed in either how the default MetadataRedirectPath is determined or how the redirect url is combined with the handler factory path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM