【问题标题】:Azure Internal Server Error on ASP.NET Core Web API ControllerASP.NET Core Web API 控制器上的 Azure 内部服务器错误
【发布时间】:2020-04-01 08:50:10
【问题描述】:

我用 React Js App 和它在 Azure 上创建了一个 ASP.NET Core Web API 控制器。尝试服务器后,我可以在 Azure 上上传,现在我的 API 出现错误。当我单击客户时,它不会给我错误,但 SQL 数据库中没有数据。

有人可以指导我如何将 DB 连接到我的 ASP.NET Core Web API 或建议我哪里做错了吗?

我尝试将数据发布/添加到客户表,但出现内部服务器错误

这是我的 appsetting.json 中的 sql 连接字符串

"ConnectionStrings": {
"DevConnection": "jdbc:sqlserver://aspapireact.database.windows.net:1433;database=ReactTask;user=*****;password=*****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;"}

Startup.cs

namespace RahulTask1
{
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();
        services.AddCors(options =>
        {
            options.AddDefaultPolicy(builder =>
            {
                builder.WithOrigins("https://aspapireact.azurewebsites.net")
                .AllowAnyMethod()
                .AllowAnyHeader();
            });
        });
        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });

        services.AddDbContext<DatabaseContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("DevConnection")));

    }

    // 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
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseRouting();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }
}
}

这是我试图调用的 API

https://aspapireact.azurewebsites.net/api/Customers

你可以在 GitHub 上看到我的代码

https://github.com/rlbrs/ASPAPIReact

在这个项目中,您将看到本地服务器连接字符串,但我已经更新了以上一个,并且与 appserver.json 相同

【问题讨论】:

    标签: reactjs azure-sql-database asp.net-core-webapi azure-sql-server


    【解决方案1】:

    您可以使用配置生成器来配置服务方法。要在任何基于环境的 appsettings 文件上从 appsettings.json 构建键值对,请将以下代码添加到 ConfigureServices 方法(这不是强制性的)

     var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false , reloadOnChange : true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    

    然后您将连接字符串的值读取为

    Configuration["ConnectionStrings:DevConnection"]
    

    PS:您使用 jdbc 连接有什么特别的原因吗?为什么不使用标准的基于点网的连接字符串?

    【讨论】:

    • 我不知道,但我认为是因为 React JS。
    • 我应该使用这个 Server=tcp:aspapireact.database.windows.net,1433;Initial Catalog=ReactTask;Persist Security Info=False;User ID=***;Password={your_password}; MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
    • 我需要一些关于这个项目的指导。如何使它工作。
    • 使用 tcp 连接字符串 POST aspapireact.azurewebsites.net/api/Customers 500(内部服务器错误)仍然收到此错误
    • 是的,它在本地运行良好,但是当我在 Azure 中上传时,没有来自 DB 的响应
    猜你喜欢
    • 2017-03-07
    • 2018-08-03
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    相关资源
    最近更新 更多