Home > Software design >  MapEndpoint Value null on running aspnet core web api
MapEndpoint Value null on running aspnet core web api

Time:09-27

I'm running my ASPNETCore Web API and getting this error! I can't find the problem and what is going wrong!? What I missed ?!

enter image description here

CodePudding user response:

namespace VPC.WebApi { public class Startup { private IWebHostEnvironment CurrentEnvironment { get; }

    public Startup(IConfiguration configuration, IWebHostEnvironment environment)
    {
        CurrentEnvironment = environment;
        Configuration = configuration;
        LoginConfiguration.Configuration = configuration;
    }

    public IConfiguration Configuration { get; }


    public void ConfigureServices(IServiceCollection services)
    {
        SetConnection(/*services*/);
        services.AddMemoryCache();
   
        services.AddMvc()
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver =
                    new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.DateParseHandling = DateParseHandling.None;
                options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
            });
        services.AddHttpClient();
       
        // The following line enables Application Insights telemetry collection.
        services.AddApplicationInsightsTelemetry();
        services.AddStackExchangeRedisCache(options =>
        {
            options.Configuration = ConnectionString.GetRedisConfiguration();
        });

        if (!CurrentEnvironment.IsDevelopment())
        {
            // The following line enables Application Insights telemetry collection.
            services.AddApplicationInsightsTelemetry(Configuration);
        }

        // Other configurations
        services.AddCors(options =>
        {
            options.AddPolicy("AllowAllOrigins",
                builder =>
                {
                    builder
                        .AllowAnyOrigin()
                        .AllowAnyHeader()
                        .AllowAnyMethod()
                        //.AllowCredentials()
                        // .WithOrigins("*")
                        // .WithMethods("*")
                        // .WithHeaders("*")
                        .SetPreflightMaxAge(TimeSpan.FromSeconds(864000));
                });
        });
       
        services.Configure<FormOptions>(o =>
        {
            o.ValueLengthLimit = int.MaxValue;
            o.MultipartBodyLengthLimit = int.MaxValue;
            o.MemoryBufferThreshold = int.MaxValue;
        });
    }

    public void SetConnection(/*IServiceCollection services*/)
    {
        var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
        // if (environment == null || environment == "") environment = "Development";
        var configuration = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables()
            .Build();
        var appConfiguration = configuration.GetSection("ConnectionString").Value;
        ConnectionString.SetConnectionString(appConfiguration);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env/*, ILoggerFactory loggerFactory*/)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            // 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.ConfigureCustomExceptionMiddleware();
        app.UseCors("AllowAllOrigins");
        app.UseHttpsRedirection();
        app.UseMiddleware<WebSocketsMiddleware>();
        app.UseAuthentication();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Production API");
            c.RoutePrefix = string.Empty;
        });

    
        app.UseHttpsRedirection();
        app.UseDefaultFiles();
        app.UseStaticFiles();
        //app.UseSpaStaticFiles();
        app.UseRouting();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });

    }
}

}

CodePudding user response:

Problem fixed by adding code below to my startup! public IConfiguration Configuration { get; }

  • Related