不确定是我做错了什么,还是一个bug,还是.net7 razor页面的变化。
当导航到带有路由的页面时。页面加载良好,url中有路由值,但没有用值填充变量。
我的页面指令是@page"/飞行/{SourceIATA ?}/{DestinationIATA ?}";并位于PagesFlightsIndex.cshtml我的变量被声明了
public class IndexModel : PageModel
{
public string? SourceIATA { get; set; }
public string? DestinationIATA { get; set; }
我正在使用program.cs.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
//builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")) , ServiceLifetime.Transient,
ServiceLifetime.Transient);
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.Configure<RequestLocalizationOptions>(ops =>
{
var cultures = new CultureInfo[] { new CultureInfo("en-US"), new CultureInfo("en-AU")
, new CultureInfo("en-GB")
, new CultureInfo("es-US")
};
ops.SupportedCultures = cultures;
ops.SupportedUICultures = cultures;
ops.DefaultRequestCulture = new Microsoft.AspNetCore.Localization.RequestCulture("en-US");
ops.RequestCultureProviders.Insert(0, new RouteSegmentRequestCultureProvider(cultures));
});
builder.Services.AddHttpClient<ITranslator, MyMemoryTranslateService>();
builder.Services.Configure<CookiePolicyOptions>(options =>
{
//gdpr
// This lambda determines whether user consent for non-essential
// cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
// requires using Microsoft.AspNetCore.Http;
options.MinimumSameSitePolicy = Microsoft.AspNetCore.Http.SameSiteMode.None;
// options.MinimumSameSitePolicy = SameSiteMode.None;
});
builder.Services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
builder.Services.AddAntiforgery(options =>
{
options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});
builder.Services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
options.LowercaseQueryStrings = true;
// options.AppendTrailingSlash = true;
});
builder.Services.AddRazorPages()
.AddRazorPagesOptions(ops => { ops.Conventions.Insert(0, new RouteTemplateModelConventionRazorPages()); })
.AddXDbLocalizer<ApplicationDbContext, MyMemoryTranslateService>(ops =>
{
ops.AutoAddKeys = false;
ops.AutoTranslate = false;
ops.UseExpressMemoryCache = false;
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
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();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseRequestLocalization();
app.MapRazorPages();
app.Run();
有没有人有任何类似的。net 7或任何建议,指出我在正确的方向将非常感激。提前谢谢。
将BindProperty
属性分配给PageModel属性,并且还必须将SupportsGet
属性指定为true
:
public class IndexModel : PageModel
{
[BindProperty(SupportsGet=true)]
public string? SourceIATA { get; set; }
[BindProperty(SupportsGet=true)]
public string? DestinationIATA { get; set; }
您可以尝试使用[FromRoute]
将路由数据与SourceIATA
和DestinationIATA
绑定:
[FromRoute]
public string? SourceIATA { get; set; }
[FromRoute]
public string? DestinationIATA { get; set; }