I'm new to Razor architecture and ASP.NET Core so my apologies if I'm missing something obvious.
I have a new web application written in the newest .NET Core framework, .NET 5.0. I have a pg folder with two Razor Pages, Contact.cshtml and Index.cshtml. I have copied code from other guides into my Startup.cs file to try and get my pages to display but I have gotten nothing but 404 errors so far. Here are the ConfigureServices and Configure methods in my Startup class.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/pg/Index", "");
});
// ----------------------------------------------
services.AddMvc();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddRazorPages().AddMvcOptions(options => options.EnableEndpointRouting = false);
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(1500);
options.Cookie.HttpOnly = true;
options.Cookie.IsEssential = true;
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Use(async (context, next) =>
{
await next();
if (context.Response.StatusCode == 404)
{
context.Request.Path = "/pg/Index";
await next();
}
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=pg}/{action=Index}/{id?}");
});
}
On starting my application I have not been able to display either of my pages. I've tried https://localhost:44328/, https://localhost:44328/pg/Index and https://localhost:44328/pg/Index.cshtml and I just get back "No webpage was found for the web address."