【问题标题】:API hosted on Azure web app returns JSON Array with out the Name .Net core 3.1托管在 Azure Web 应用程序上的 API 返回 JSON 数组,但没有 Name .Net core 3.1
【发布时间】:2020-01-28 21:02:57
【问题描述】:

我有一个 Web API Get。这会生成正确的 JSON 响应。当我在 Azure 上运行代码时,应用程序会删除 questions 名称。非常感谢任何帮助!

 [HttpGet]
    public ActionResult<IEnumerable<Question>> Get()
    {
        var questions = _repo.GetQuestions();
        return Ok(new { questions });
    }

本地结果

    {
    "questions": [
        {
            "property": "test",
            "required": true,
            "type": "string",
            "label": "test ID",
            "constraints": {
                "minSize": "9",
                "maxSize": "10"
            }
        },
        {
            "property": "Email",
            "required": true,
            "type": "string",
            "label": "Email Address",
            "constraints": {
                "minSize": "1",
                "maxSize": "128"
            }
        }
    ]
}

天蓝色

[
    {
        "property": "test",
        "required": true,
        "type": "string",
        "label": "test ID",
        "constraints": {
            "minSize": "9",
            "maxSize": "10"
        }
    },
    {
        "property": "Email",
        "required": true,
        "type": "string",
        "label": "Email Address",
        "constraints": {
            "minSize": "1",
            "maxSize": "128"
        }
    }
]

启动

public void ConfigureServices(IServiceCollection services)
    {
        var connectionString = string.Empty;

        services.AddControllers(configure =>
        {
            configure.ReturnHttpNotAcceptable = true;//makes sure that content type is requested
        });

        services.AddDbContext<YLS_FAASTContext>(options =>
        {
            options.UseSqlServer(Configuration.GetConnectionString(connectionString ));
        });

        services.AddScoped<IAPIRepo, APIRepo>();

        services.AddAuthentication("BasicAuthentication")
            .AddScheme<AuthenticationSchemeOptions, BasicAuthenticationHandler>("BasicAuthentication", null);

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {//prod only show 500 error
            app.UseExceptionHandler(appBuilder =>
            {
                appBuilder.Run(async context =>
                {
                    context.Response.StatusCode = 500;
                    await context.Response.WriteAsync("An unexpected error has occured.  Try again later.");
                });
            });
        }


        app.UseRouting();

        app.UseAuthentication();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

【问题讨论】:

    标签: c# .net azure asp.net-core


    【解决方案1】:

    看起来你在这里遗漏了一些东西。我尝试了以下代码。

    [ApiController]
    [Route("[controller]")]
    public class QuestionsController : ControllerBase
    {
        [HttpGet]
        public ActionResult<IEnumerable<Question>> Get()
        {
            var questions = new List<Question>(2);
            questions.Add(new Question()
            {
                property = "test",
                required = true,
                type = "string",
                label = "Test Id",
                constraints = new Constraints() { maxSize = "10", minSize = "9" }
            });
    
            questions.Add(new Question()
            {
                property = "Email",
                required = true,
                type = "string",
                label = "Email Address",
                constraints = new Constraints() { maxSize = "128", minSize = "1" }
            });
    
            return Ok(new { questions });
        }
    }
    

    而且我总是从本地和 azure 得到相同的响应。

    这是来自 Azure 应用服务的响应。

    这是Startup.cs 文件。

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
    
            app.UseHttpsRedirection();
    
            app.UseRouting();
    
            app.UseAuthorization();
    
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
    

    【讨论】:

    • 感谢分享。您是否使用 .Net Core 3.1 获得此结果?
    • 是的。我使用.Net core 3.1
    • 您能分享一下您的创业公司吗?我用我的启动更新了代码。谢谢。
    • 我在 Startup.cs 中没有任何特定内容 - 我只是创建了一个 API 项目,所有这些
    猜你喜欢
    • 2021-09-19
    • 2020-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多