1
votes

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."

EDIT: Here's my Solution Explorer: Solution Explorer

4

4 Answers

2
votes

You could check the official document:

The runtime looks for Razor Pages files in the Pages folder by default.

If you do not want to change the razor pages folder, add the following code to your Startup.cs:

services.AddMvc().AddRazorPagesOptions(opt => {
    opt.RootDirectory = "/pg";
});

Besides, remember to add razor pages endpoint:

app.UseEndpoints(endpoints =>
{
    endpoints.MapRazorPages();
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=pg}/{action=Index}/{id?}");
});
1
votes

You need to add Razor page endpoints:

app.UseEndpoints(endpoints =>
    {
        ...
        endpoints.MapRazorPages();
    });

Your special case for 404 might need to move lower down as well; whether it’s going to be a 404 isn’t established yet where you have it.

0
votes

I've tried to illustrate your problem using your configurations. And it is working fine for me. Maybe your folder structure is bad or you have not added the required controller.

Since you are using MVC you need to put the index.razor file in the Views folder. This is how the folder structure should look like -

enter image description here

0
votes

You need to create a folder named Pages , and add all razor pages inside that Pages folder.

In startup.cs under configure method add below code

app.UseEndpoints(endpoints =>
{
   ..
   endpoints.MapRazorPages();
});