【发布时间】: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