简体   繁体   English

OWIN中的Nancy和WebAPI自托管路由冲突

[英]Nancy and WebAPI in OWIN self host conflicting routes

I'm doing some work with OWIN for the first time and am trying to set things up so that I can leverage Web API with attributed routing for backend services and I'd like to use Nancy as a simple file server to serve up my angular SPA code. 我第一次与OWIN合作,并尝试进行设置,以便可以利用Web API和后端路由使用属性路由,并且我希望使用Nancy作为简单的文件服务器来提供服务。 SPA代码。

My OWIN startup code: 我的OWIN启动代码:

public class Startup : IOwinAppBuilder
{
    public void Configuration(IAppBuilder appBuilder)
    {
        var config = new HttpConfiguration();

        config.MapHttpAttributeRoutes();
        //FormatterConfig.ConfigureFormatters(config.Formatters);

        config.Formatters.Clear();
        config.Formatters.Add(new JsonMediaTypeFormatter());
        config.Formatters.JsonFormatter.SerializerSettings =
        new JsonSerializerSettings
        {
            ContractResolver = new CamelCasePropertyNamesContractResolver()
        };

        //appBuilder.UseNancy();
        appBuilder.UseWebApi(config);
    }
}

I have a Test controller set up with some endpoints appropriately decorated for attribute routing. 我有一个测试控制器,其中设置了一些为属性路由适当装饰的端点。 At this point running the app and attempting to hit one of those endpoints returns the expected results. 此时,运行应用程序并尝试命中这些端点之一将返回预期结果。

I then tried to add Nancy to the equation. 然后,我尝试将Nancy添加到公式中。 I added a single Nancy Module with the intention that it would serve up the angular content: 我添加了一个Nancy Module,旨在满足角度的需求:

public class HomeModule : NancyModule
{
    public HomeModule()
    {
        Get["/"] = _ => View["home"];
    }
}

Uncommenting the appBuilder.UseNancy() line in the Startup and running the server works perfect to get the home view. 在启动中取消对appBuilder.UseNancy()行的注释并运行服务器可以完美地获得主视图。 However, now when I attempt to hit one of the API endpoints, Nancy seems to be kicking in and handling the request and returning me a 404 page instead of the API result. 但是,现在,当我尝试访问API端点之一时,Nancy似乎正在介入并处理请求,并向我返回404页面而不是API结果。 Is there a way to make Nancy and Web API play nicely together such that their routes will not conflict like this? 有没有一种方法可以使Nancy和Web API很好地配合使用,以使它们的路由不会像这样发生冲突?

Did you test the PerformPassThrough or PassThroughWhenStatusCodesAre in UseNancy() method? 您是否在UseNancy()方法中测试了PerformPassThrough或PassThroughWhenStatusCodesAre?

It is documented in Conditional pass-through section of Hosting nancy with owin. 使用owin在Hosting nancy的条件通过部分中进行了记录。 ( https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#conditional-pass-through ) https://github.com/NancyFx/Nancy/wiki/Hosting-nancy-with-owin#conditional-pass-through

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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