繁体   English   中英

MapEndpoint 值 null 在运行 aspnet 核心 web api

[英]MapEndpoint Value null on running aspnet core web api

我正在运行我的 ASPNETCore Web API 并收到此错误? 我找不到问题,出了什么问题?? 我错过了什么?!

在此处输入图像描述

命名空间 VPC.WebApi { public class 启动 { 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();
        });

    }
}

}

通过在我的启动中添加以下代码解决了问题! public IConfiguration Configuration { get; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM