gpt4 book ai didi

angular - 无法识别的配置部分 system.webServer。 (对于 Angular 核心 - Angular)

转载 作者:行者123 更新时间:2023-12-04 08:39:17 28 4
gpt4 key购买 nike

我正在尝试在 Asp.Net 核心 api 文件中集成 Angular ,但是在访问/调用 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");
}
});
}
}
}
错误消息:
enter image description here
我显示上面的配置是因为 中显示的错误
我错过了什么请分享我相关的想法/链接和代码。

最佳答案

您的开发机器上缺少 IIS 服务器的 URL 重写扩展,因此无法识别配置的重写规则部分。
您有 2 个选择:

  • 安装 URL 重写扩展
    https://www.iis.net/downloads/microsoft/url-rewrite
  • 删除或注释掉 web.config 中的重写规则部分。也许您根本不需要开发机器上的 URL 重写功能。
  • 关于angular - 无法识别的配置部分 system.webServer。 (对于 Angular 核心 - Angular),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64641984/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com