![](/img/trans.png)
[英]Get Current URL as soon as available program.cs / startup.cs in ASP.NET Core
[英].NET Core program.cs and Startup.cs not hit
我有一個應用程序的大問題。 當我使用 IIS Express 時,一切正常,但如果我使用 IIS 啟動應用程序(使用或不使用 Visual Studio), Program.cs
和Startup.cs
將被忽略,因此應用程序無法運行。
.NET Core 2.2 和 .NET Core 3.1 以及 Razor 頁面或 MVC 項目都會發生這種情況。
奇怪的是,IIS一直工作到昨天,我沒有做任何改動,只是兩天之間重啟了一次電腦。 這在我的筆記本電腦和台式電腦上都有。
我不明白為什么,但這讓我發瘋。 你對解決這個問題有什么建議嗎?
程序.cs
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args)
.Build()
.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
啟動.cs
public const string GenericCookieScheme = "XXX";
public const string AuthSecret = "XXX";
public Startup(IConfiguration configuration)
{
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.AddRazorPages(options =>
{
options.Conventions.AuthorizePage("/Pages");
options.Conventions.AllowAnonymousToFolder("/Pages/Login");
});
services.AddSession();
services.AddControllersWithViews().AddRazorRuntimeCompilation();
services.AddSingleton(Configuration.GetSection("AppSettings").Get<AppSettings>());
#region SERVER
services.AddEntityFrameworkSqlServer()
.AddDbContext<DbConfigContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("ConfigContainer")));
services.AddEntityFrameworkSqlServer()
.AddDbContext<DbDataContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DataContainer")));
services.AddScoped<ITenantProvider, TenantProvider>();
services.AddScoped<IUserProvider, UserProvider>();
services.AddTransient<IDbContextFactory, DbContextFactory>();
DbDataContext.Init();
#endregion
#region AUTHENTICATION
services.AddAuthentication(o =>
{
o.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
o.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.AccessDeniedPath = new PathString("/Login");
options.LoginPath = new PathString("/Login");
});
#endregion
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = false;
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
// 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.UseHttpsRedirection();
var defaultCulture = new CultureInfo("it-IT");
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture(defaultCulture),
SupportedCultures = new List<CultureInfo> { defaultCulture },
SupportedUICultures = new List<CultureInfo> { defaultCulture }
};
app.UseRequestLocalization(localizationOptions);
app.UseHttpsRedirection();
app.UseSession();
app.UseAuthentication();
app.UseStaticFiles();
app.UseRequestLocalization("it-IT");
app.UseRouting();
app.UseRouter(r =>
{
r.MapGet(".well-known/acme-challenge/{id}", async (request, response, routeData) =>
{
var id = routeData.Values["id"] as string;
var file = Path.Combine(env.WebRootPath, ".well-known", "acme-challenge", id);
await response.SendFileAsync(file);
});
});
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
var t = serviceScope.ServiceProvider.GetService<IHttpContextAccessor>();
#region CONFIG CONTAINER
if (!serviceScope.ServiceProvider.GetService<DbConfigContext>().AllMigrationsApplied())
{
serviceScope.ServiceProvider.GetService<DbConfigContext>().Database.Migrate();
}
serviceScope.ServiceProvider.GetService<DbConfigContext>().EnsureSeeded(env.WebRootPath);
#endregion
#region DATA CONTAINER
var dbContextFactory = serviceScope.ServiceProvider.GetService<IDbContextFactory>();
//var allTenants = serviceScope.ServiceProvider.GetService<SigfridAppConfigContext>().Tenants.First();
var context = dbContextFactory.CreateDbContext(Configuration);
if (!context.AllMigrationsApplied())
{
context.Database.Migrate();
}
//serviceScope.ServiceProvider.GetService<DbDataContext>().EnsureSeeded(Guid.Parse("A2DDFB53-3221-41E7-AD27-F3CD70EC5BAF"));
#endregion
}
}
請檢查您的 IIS 是否安裝hosting bundle for ASP.NET Core
。
在這里檢查:
如果沒有,請在此處下載並安裝它:
請檢查您站點的根文件夾中是否有名為web.config
的文件:
它的內容應該是這樣的:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Aiursoft.Account.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
檢查部分: < <system.webServer>
> 下的<aspNetCore>
> 。
好的,我發現了問題。 重復的路線。 我有一個名為“/dashboard”的路由,controller 文件夾中有一個 controller,api 文件夾中有一個相同的路由。 我不知道為什么沒有“重復路線”的錯誤,但現在可以了。 很抱歉在一個愚蠢的問題上浪費了你的時間:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.