asp.net-core - 在 ASP.Net Core 中将 swashbuckle swagger 与 odata 集成

标签 asp.net-core odata swashbuckle

关闭。这个问题需要debugging details .它目前不接受答案。












想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。

2年前关闭。



Improve this question




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

我无法整合为 odata 提供的路线。

我有以下配置,但收到一般错误。

Configuration

这是错误

this is the error

最佳答案

将 OData 添加到我们的 .Net Core 项目时,我们遇到了同样的问题。 this post 上的代码片段中显示的解决方法修复了 Swagger UI 加载时的 API 错误。

据我所知,AspNetCore 的 Swashbuckle 不支持 OData。因此,在上面的链接中添加解决方法代码后,我们的 Swagger UI 可以工作,但没有显示任何 OData 端点。

来自链接的代码片段:

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();
        });
    }
}

关于asp.net-core - 在 ASP.Net Core 中将 swashbuckle swagger 与 odata 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50940593/

相关文章:

asp.net-core - 如何在.net Core中启用迁移?

c# - 在 Asp.Net Core 中使用 POST 方法重定向到 URL

service - 在SAPUI5中调用XSJS文件在HANA上写入数据

entity-framework - 如何使用 OData 在单个 POST 请求中正确创建和链接一对一关系

javascript - 使用 odata 自定义 orderBy?

c# - ASP.NET Core 2.0 - 动态定义文档模型

c# - 如何在 AspNetCore 中定义 OpenApi 的文件响应

asp.net-core - Redis 不会从数据库中删除记录

c# - 如何使用 Swashbuckle 记录查询字符串参数?

authentication - 在 ASP.NET 5 中动态添加角色以授权 Controller 的属性