Ron*_*dau 10 c# iis kestrel-http-server asp.net-core
在从 .net core 2.2 切换到 3.0,然后在本地切换到 3.1 之后,我们切换到端点路由。我有以下路线:
app.UseEndpoints(endpoints =>
{
// Using this for asp.net core identity
endpoints.MapRazorPages();
// Mapping 2 routes, one private, one public but that we don't want localized so in both cases was simpler to create an area
endpoints.MapAreaControllerRoute("Back", "Back", "back/{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute("Photo", "Photo", "photo/{controller=Photo}/{action=Index}/{id?}");
// The default mapping for our front office, this works just fine in iis express
endpoints.MapControllerRoute("default", "{lang:lang}/{controller=Home}/{action=Index}/{id?}");
// Fallback is used mainly to redirect you from / to /defaultlanguage
endpoints.MapFallbackToController("WrongEndpoint","Home");
});
Run Code Online (Sandbox Code Playgroud)
我使用以下博客的代码来检查我在两种环境中是否具有相同的端点:https : //dev.to/buhakmeh/endpoint-debugging-in-asp-net-core-3-applications-2b45
在这两种情况下,我都有预期的行为(我通过将该控制器放在一个有效的区域对其进行了测试):我希望采用“默认”的路由优先级为 3,而回退为 2147483647,因此它显然被跳过了。
键入全名以忽略可选组件(如 mydomain/fr/Home/Index)仍会将我重定向到 IIS 下的回退控制器。
我不知道为什么它在 IIS 下的行为会与 IIS Express 不同。另请注意,剃刀页面和区域控制器都可以正常工作,只是默认路由失败并回退到良好状态,即回退。
删除回退也不会使默认控制器工作。
编辑 1:根据评论中的要求,完整的 startup.cs 波纹管
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using FranceMontgolfieres.Models;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Routing;
namespace FranceMontgolfieres
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
Utilities.ConfigurationUtils.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.AddSingleton<IConfiguration>(Configuration);
services
.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services
.AddDbContext<FMContext>(options => options
.UseLazyLoadingProxies(true)
.EnableSensitiveDataLogging()
.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
services
.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<FMContext>();
services
.AddMemoryCache();
services.AddDistributedSqlServerCache(options =>
{
options.ConnectionString = Configuration.GetConnectionString("SessionConnection");
options.SchemaName = "dbo";
options.TableName = "SessionCache";
});
services.AddHttpContextAccessor();
services
.AddSession(options => options.IdleTimeout = TimeSpan.FromMinutes(30));
services
.AddControllersWithViews()
.AddRazorRuntimeCompilation();
services.AddRazorPages();
services.Configure<RouteOptions>(options =>
{
options.ConstraintMap.Add("lang", typeof(LanguageRouteConstraint));
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Called by asp.net core by convention")]
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
// TODO CRITICAL Remove before production
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
// TODO CRITICAL ENABLE BEFORE PRODUCTION
// app.UseHsts();
}
//app.UseHttpsRedirection();
app.UseRouting();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapAreaControllerRoute("Back", "Back", "back/{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute("Photo", "Photo", "photo/{controller=Photo}/{action=Index}/{id?}");
endpoints.MapControllerRoute("default", "{lang:lang}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapFallbackToController("WrongEndpoint","Home");
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 2 :根据 Lex Li 的评论,在部署的服务器上添加了 web 配置
<?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=".\PROJECTNAMEEDITEDOUT.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
</system.webServer>
</location>
</configuration>
<!--ProjectGuid: THERE IS A GUID HERE I REMOVED-->
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1119 次 |
| 最近记录: |