Azure Web应用程序上托管的API返回名称为.Net core 3.1的JSON数组

问题描述 投票:0回答:1

我有一个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"
            }
        }
    ]
}

Azure

[
    {
        "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个回答
0
投票

好像您在这里丢失了一些东西。我尝试了以下代码。

[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应用服务的响应。

JSON Response from Azure

© www.soinside.com 2019 - 2024. All rights reserved.