我正在尝试在Asp.Net核心api文件中整合角度,但是在访问/调用api(.core)时,显示以下错误。已经通过了不同的链接,不知道确切的原因,这会导致错误。
//
"error": {
"innerExceptionMessage": "Failed to read the config section for authentication providers.",
"message": "The type initializer for 'Microsoft.Data.SqlClient.SqlAuthenticationProviderManager' threw an exception.",
"exception": "TypeInitializationException"
}
}
// in Inner Exception
Unrecognized configuration section system.webServer. (D:\WorkSpace\API\API_New\bin\Debug\netcoreapp3.1\project.dll.config line 4)
相关代码如下:
app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url="./*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_URI}" pattern="^/(api)" negate="true" />
</conditions>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
Environment = env;
}
public IConfiguration Configuration { get; }
private IWebHostEnvironment Environment { get; }
public string MyAllowSpecificOrigins { get; private set; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("http://localhost:50040/"
).AllowAnyMethod().AllowAnyOrigin().AllowAnyHeader();
});
});
services.AddControllers();
string constring = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<SRCSContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
// In Dev Mode we will use the ClientApp at the root level
if (Environment.IsDevelopment())
{
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "SRCS-Web/dist";
});
}
else
{
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "publish/SRCS-Web/dist";
});
}
}
// 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.UseMiddleware<ExceptionHandler>();
app.UseHttpsRedirection();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseCors();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "SRCS-Web";
if (env.IsDevelopment())
{
//Configure the timeout to 3 minutes to avoid "The Angular CLI process did not start listening for requests within the timeout period of 50 seconds." issue
spa.Options.StartupTimeout = new TimeSpan(0, 6, 30);
spa.UseAngularCliServer(npmScript: "start");
}
});
}
}
}
答案 0 :(得分:2)
您缺少开发计算机上IIS服务器的URL重写扩展名,因此无法识别配置的重写规则。
您有2个选择:
安装URL重写扩展名 https://www.iis.net/downloads/microsoft/url-rewrite
删除或注释掉web.config中的重写规则部分。也许您根本不需要开发机器上的URL重写功能。