将swashbuckle swagger与odata集成到ASP.Net Core中 [英] Integrate swashbuckle swagger with odata in ASP.Net Core

查看:242
本文介绍了将swashbuckle swagger与odata集成到ASP.Net Core中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在asp.net核心中同时实现(swagger和odata),但是它不起作用.

I have tried to implement both ( swagger and odata ) in asp.net core, but it's not working.

我无法集成为odata提供的路由.

I'm unable to integrate the route given for odata.

我具有以下配置,并且收到一般错误.

I have the following Configuration and I receive a generic error.

这是错误

推荐答案

将OData添加到.Net Core项目时,我们遇到了同样的问题. 这篇文章上的代码段中所示的变通办法已修复我们的API错误( s)Swagger用户界面加载时.

We ran into the same issue when adding OData to our .Net Core project. The workarounds shown in the code snippet on this post fixed our API error(s) when Swagger UI loads.

据我所知,Swashbuckle for AspNetCore不支持OData.因此,在上面的链接中添加了变通方法代码后,我们的Swagger UI可以运行,但是没有OData端点显示.

As far as I can tell, OData isn't supported in Swashbuckle for AspNetCore. So after adding the workaround code in the link above, our Swagger UI works, but none of the OData endpoints show.

链接中的代码段:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        services.AddOData();

        // Workaround: https://github.com/OData/WebApi/issues/1177
        services.AddMvcCore(options =>
        {
            foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
            foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {                
        var builder = new ODataConventionModelBuilder(app.ApplicationServices);

        builder.EntitySet<Product>("Products");

        app.UseMvc(routebuilder => 
        {
            routebuilder.MapODataServiceRoute("ODataRoute", "odata", builder.GetEdmModel());

            // Workaround: https://github.com/OData/WebApi/issues/1175
            routes.EnableDependencyInjection();
        });
    }
}

这篇关于将swashbuckle swagger与odata集成到ASP.Net Core中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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