Unrecognized configuration section system.webServer. (For Angular Core - Angular)

Viewed 206

i'm trying to intergrate the angular in Asp.Net core api file , but while accessing / calling the api (.core) the below error is showing. Already went through the different links , dont know the exact reason, which causes the error.

//
 "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)

Relavant Code are shown below:

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");
                }
            });
        }
    }
}

Error Message: enter image description here I 'm showing the above config because the error showing in the <system.webServer> What I'm missing please share me relavant ideas/links and codes.

1 Answers

You are missing URL Rewrite extension for IIS server on your development machine, thus rewrite rules part of configuration cannot be recognized.

You have 2 options:

  1. Install URL Rewrite Extension https://www.iis.net/downloads/microsoft/url-rewrite

  2. Remove or comment out rewrite rules section in the web.config. Maybe you don't need URL Rewrite functionality on development machine at all.

Related