gpt4 book ai didi

asp.net-core-2.0 - Asp.net Core2 Web Api 发布到 IIS 8.5 总是在访问时给出 404

转载 作者:行者123 更新时间:2023-12-01 05:56:13 27 4
gpt4 key购买 nike

我正在开发一个 Asp.Net-Core-2.0 Web API 项目。我将 Web API 发布到 IIS,现在当我尝试访问它时,它给了我一个 404 Error所有 Controller 中的消息。

我试图用谷歌搜索一切,但没有任何帮助。

如果有人需要任何信息,请在问题下发表评论,我会更新我的问题。

Program.cs 文件

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}

Startup.cs 文件
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();

//Configuration = configuration;
}
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped<AuthRepository>();
services.AddSingleton<IKapanRepository, KapanRepository>();
services.AddSingleton<IDepartmentRepository, DepartmentRepository>();
services.AddSingleton<IPriorityRepository, PriorityRepository>();
services.AddSingleton<IPlanRepository, PlanRepository>();
services.AddSingleton<IRuleTemplateRepository, RuleTemplateRepository>();
services.AddSingleton<IDamageReportRepository, DamageReportRepository>();
services.AddSingleton<IDashBoardRepository, DashBoardRepository>();
services.AddSingleton<IPacketRepository, PacketRepository>();
services.AddSingleton<IEmployeeRepository, EmployeeRepository>();
services.AddSingleton<IPriceCalculatorRepository, PriceCalculatorRepository>();
services.AddSingleton<ISearchRepository, SearchRepository>();
services.AddSingleton<IUserInfoRepository, UserInfoRepository>();
services.AddSingleton<IOverLossRepository, OverLossRepository>();

// Container could be configured via services as well.
// Just be careful not to override registrations
services.AddDbContext<ApplicationContext>(opts =>
opts.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

// Add framework services.
services.AddMvc();
services.AddIdentity<ApplicationUser, IdentityRole>(options => {
options.Password.RequireNonAlphanumeric = false;
})
.AddEntityFrameworkStores<ApiDbContext>()
.AddDefaultTokenProviders();
services.AddDbContext<ApiDbContext>(options => options.UseSqlServer(Settings.ConnectionStringAuth));

// return 401 instead of redirect to login
services.ConfigureApplicationCookie(options => {
options.Events.OnRedirectToLogin = context => {
context.Response.Headers["Location"] = context.RedirectUri;
context.Response.StatusCode = 401;
return Task.CompletedTask;
};
});

services.AddAuthentication(sharedOptions => {
sharedOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(cfg => {
cfg.RequireHttpsMetadata = false;

cfg.TokenValidationParameters = new TokenValidationParameters() {
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
LifetimeValidator = CustomLifetimeValidator,
ValidIssuer = Configuration["TokenAuthentication:Issuer"],
ValidAudience = Configuration["TokenAuthentication:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration["TokenAuthentication:client_secret"]))
};

cfg.Events = new JwtBearerEvents {
OnAuthenticationFailed = context => {
Console.WriteLine("OnAuthenticationFailed: " +
context.Exception.Message);
return Task.CompletedTask;
},
OnTokenValidated = context => {
Console.WriteLine("OnTokenValidated: " +
context.SecurityToken);
return Task.CompletedTask;
}
};

});
services.Configure<IISOptions>(options =>
{
options.ForwardClientCertificate = false;
});
}

private bool CustomLifetimeValidator(DateTime? notBefore, DateTime? expires, SecurityToken securityToken, TokenValidationParameters validationParameters)
{
if (expires != null) {
return expires > DateTime.UtcNow;
}
return false;
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();

app.UseMiddleware<TokenProviderMiddleware>();
app.UseMiddleware<RefreshTokenProviderMiddleware>();
app.UseAuthentication();
app.UseMvc();
}
}

任何帮助将不胜感激。

最佳答案

是的,我知道了..

在找到谷歌后,没有什么能帮助我。
自己做了之后而不是服用Web Api我正在做的项目Web Applicaton项目并在 IIS 上发布它就像一个魅力

尝试一下。

关于asp.net-core-2.0 - Asp.net Core2 Web Api 发布到 IIS 8.5 总是在访问时给出 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49208612/

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