Home > Enterprise >  Razor page routing convetion not working in .net 7. vs2022
Razor page routing convetion not working in .net 7. vs2022

Time:12-02

Not sure if it something im doing wrong or a bug or a change in .net7 razor pages.

when navigation to a page with a route. the page loads fine and has the route values in the url but does not populate the variables with the values.

my page directive is @page "/flights/{SourceIATA?}/{DestinationIATA?}" and is located in Pages\Flights\Index.cshtml and my varibles are declared.

  public class IndexModel : PageModel
    {

        public string? SourceIATA { get; set; }

        public string? DestinationIATA { get; set; }

am using 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();

Has anyone had anything similar with .net 7 or any advices to point me in the right direction would be greatly appreciated. thanks in advance.

CodePudding user response:

You can try to use [FromRoute] to bind the route data with SourceIATA and DestinationIATA :

[FromRoute]
public string? SourceIATA { get; set; }

[FromRoute]
public string? DestinationIATA { get; set; }

CodePudding user response:

Assign the BindProperty attribute to your PageModel properties and you must also specify the SupportsGet property as true:

public class IndexModel : PageModel
{
    [BindProperty(SupportsGet=true)]
    public string? SourceIATA { get; set; }

    [BindProperty(SupportsGet=true)]
    public string? DestinationIATA { get; set; }
  • Related