无法获取使用Owin工作时的ASP.NET Web API 2帮助页面 [英] Can't get ASP.NET Web API 2 Help pages working when using Owin

查看:240
本文介绍了无法获取使用Owin工作时的ASP.NET Web API 2帮助页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经安装了正确的包网页API 2

 安装封装Microsoft.AspNet.WebApi.HelpPage  -  pre

但帮助区域没有被映射,并返回404(网页API工作的罚款)。我使用Microsoft.Owin.Host.SystemWeb作为主机。下面是我启动code。

 公共类启动
    {
        公共无效配置(IAppBuilder应用程序)
        {
            //所需的MVC领域的新HttpConfiguration()不与MVC工作
            无功配置= GlobalConfiguration.Configuration;            AreaRegistration.RegisterAllAreas();            WepApiStartup.Configure(配置);            app.UseWebApi(配置);        }
    }


解决方案

发现了很多挖/试错后的溶液。这个问题很好说明如下: HTTP://aspnetwebstack.$c$cplex.com/discussions/ 453068

UseWebApi和UseHttpMessageHandler不叫下一页OWIN的中间件比404.这意味着如果你使用UseWebApi就是这样,下一个是从来没有因此叫你不能与任何其他的中间件使用它(南希或Web API,用于帮助其他页面例子)。

感谢@aliostad补丁:
<一href=\"https://github.com/aliostad/CacheCow/blob/master/samples/UsingCacheCowWithNancyAndOwin/HttpMessageHandlerAdapterModified.cs#L43\" rel=\"nofollow\">https://github.com/aliostad/CacheCow/blob/master/samples/UsingCacheCowWithNancyAndOwin/HttpMessageHandlerAdapterModified.cs#L43

您可以得到它工作正常。我希望球队合并本拉入请求为UseWebApi打破Owin设计目标海事组织。

更新2014年2月13日

我已经写了一个Owin扩展解决此:

 内部静态无效UseWebApiAndHelp(这IAppBuilder应用程序,HttpConfiguration配置)
{
    WepApiStartup.Configure(配置);    app.UseHandlerAsync((请求,响应下一个)= GT;
    {
        如果(Request的==/)//app.Map使用正则表达式排除列表会在这里更好,所以它不会激发每个请求
        {
            response.Status code = 301;
            response.SetHeader(位置,/帮助);
            返回Task.FromResult(0);
        }        返回下一个();
    });    //地图的帮助路径和下一个被调用强制
    app.Map(/帮助的AppBuilder =&GT; appbuilder.UseHandlerAsync((请求,响应下一个)=&gt;接下来()));    app.UseWebApi(配置);
}

2015年更新07月01日

您还可以使用托管的的WebAPI代替MVC,这是伟大的自我主机<一个帮助页面href=\"http://blogs.msdn.com/b/yaohuang1/archive/2012/12/20/making-asp-net-web-api-help-page-work-on-self-hosted-services.aspx\" rel=\"nofollow\">http://blogs.msdn.com/b/yaohuang1/archive/2012/12/20/making-asp-net-web-api-help-page-work-on-self-hosted-services.aspx

2015年更新9月10日

有关网页API我试图@红叶太阳的答案,它也能工作,遵循什么@gspatel说,通过改变HelpPageAreaRegistration.RegisterArea和 HelpController的构造。我的解决方法的工作原理,以及因此选择任何一个最适合您的情况。

不过使用UseWebApi与其他中间件时,我仍然得到这个问题,它不调用next()的(好像用IIS不能自主机时才会发生)。我发现我的映射路径和下一个被调用的强迫解决方法是对所有Owin中间件南希,Simple.Web等。

一个有效的解决方法

更新2016年1月13日

我已经开发Owin中间件来产生我们知道,爱是完全解决了这个问题的ASP.NET Web API帮助页面。 <一href=\"http://www.dalsoft.co.uk/blog/index.php/2016/01/13/introducing-dalsoft-webapi-helppage-help-pages-for-asp-net-web-api-working-on-owin-self-host/\"相对=nofollow>我的博客文章解释

I've installed the correct package for Web Api 2

Install-Package Microsoft.AspNet.WebApi.HelpPage -Pre 

But the help area is not being mapped and is returning 404 (Web Api working fine). I'm using Microsoft.Owin.Host.SystemWeb as the host. Below is my Startup code.

    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            //Required for MVC areas new HttpConfiguration() doesn't work with MVC
            var config = GlobalConfiguration.Configuration;

            AreaRegistration.RegisterAllAreas();

            WepApiStartup.Configure(config);

            app.UseWebApi(config);

        }
    }

解决方案

Found the solution after a lot of digging/trial and error. The issue is well described here: http://aspnetwebstack.codeplex.com/discussions/453068

UseWebApi and UseHttpMessageHandler don't call Next OWIN's middleware other than for 404. This means if you use UseWebApi that's it, Next is never called therefore you can't use it with any other middleware (Nancy or Web Api Help pages for example).

Thanks to @aliostad patch: https://github.com/aliostad/CacheCow/blob/master/samples/UsingCacheCowWithNancyAndOwin/HttpMessageHandlerAdapterModified.cs#L43

You can get it working as expected. I hope the team merge the pull request for this as UseWebApi breaks the Owin design goals IMO.

Update 13 Feb 2014

I've written an Owin extension to workaround this:

internal static void UseWebApiAndHelp(this IAppBuilder app, HttpConfiguration config)
{
    WepApiStartup.Configure(config);

    app.UseHandlerAsync((request, response, next) =>
    {
        if (request.Path == "/") //app.Map using a regex exclude list would be better here so it doesn't fire for every request
        {
            response.StatusCode = 301;
            response.SetHeader("Location", "/Help");
            return Task.FromResult(0);
        }

        return next();
    });

    // Map the help path and force next to be invoked
    app.Map("/help", appbuilder => appbuilder.UseHandlerAsync((request, response, next) => next()));

    app.UseWebApi(config);    
}

Update 01 July 2015

You can also host the help pages using WebApi instead of MVC, which is great for self host http://blogs.msdn.com/b/yaohuang1/archive/2012/12/20/making-asp-net-web-api-help-page-work-on-self-hosted-services.aspx

Update 10 September 2015

For Web Api I tried @hongye-sun answer and it works too, follow what @gspatel says by changing HelpPageAreaRegistration.RegisterArea and the HelpController's constructor. My workaround works as well so pick whatever one works best for your situation.

However I'm still getting the issue when using UseWebApi with other middleware and it not invoking Next() (seems to only happen when using IIS not self host). I've found my workaround of mapping the path and forcing next to be invoked is a valid workaround for all Owin middleware Nancy, Simple.Web etc.

Update 13 Jan 2016

I've developed Owin middleware to generate the ASP.NET Web API Help pages we know and love that completely solves this problem. My blog post explains the background to this issue in detail

这篇关于无法获取使用Owin工作时的ASP.NET Web API 2帮助页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆