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

标签 angular asp.net-core

我正在尝试在 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/

    相关文章:

    angular - 效果 "AuthEffects.authLogin$"发送无效操作 : undefined

    ASP.NET 核心 : How to login with “UserName” instead of “Email” ?

    c# - 将 XmlComment 用于 swagger api 帮助器页面

    .net - ASP.NET TicketDataFormat.Unprotect(cookieValue) 返回 null

    angular - typescript 中的多种类型断言

    javascript - 如何在for循环中使用angular http rest api保证顺序?

    javascript - Angular 购物车 - 本地存储服务和订阅更改

    angular - 在浏览器的输入中检测 Ctrl + C 和 Ctrl + V

    asp.net - ASP.NET Core 2 中的路由本地化

    c# - 使用 Core 2.2 本地化的 Razor 页面不起作用