How do you set the default document programmatically in the Program.cs file of an ASP.NET Core Web App (Razor Pages)

Dyllan Jones 20 Reputation points
2024-06-17T19:45:33.41+00:00

I have created a new app using the ASP.NET Core Web App (Razor Pages) template in Visual Studio 2022. I have the app linked up to Azure AD so when a user goes to the site it redirects them to authenticate through AAD before directing them back to the main page (Index.cshtml). When running on my local machine, it works fine. However, I installed it on a web server in IIS and when I go to the site, I get a "403.14 Forbidden error: A default document is not configured for the requested URL, and directory browsing is not enabled on the server". Looking online it looks like I need to include some routing in the Program.cs file for a default document since IIS will not allow you to set a .cshtml file as the default document. All the examples I see online look to be referencing MVC and the Startup.cs file which I do not have in this project. How do I set the default document for this project type so IIS can host the web app?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2024-06-17T19:45:33.4266667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To set the default document programmatically in an ASP.NET Core Web App (Razor Pages), you can add a middleware in the Configure method of the Startup.cs file. However, since you mentioned that you do not have a Startup.cs file in your project, you can try adding the following code to the Program.cs file:

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Hosting;
    
    namespace YourNamespace
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                CreateHostBuilder(args).Build().Run();
            }
    
            public static IHostBuilder CreateHostBuilder(string[] args) =>
                Host.CreateDefaultBuilder(args)
                    .ConfigureWebHostDefaults(webBuilder =>
                    {
                        webBuilder.UseStartup<Startup>();
                        webBuilder.UseDefaultServiceProvider(options =>
                            options.ValidateScopes = false);
                        webBuilder.UseWebRoot("wwwroot");
                        webBuilder.UseDefaultFiles(new DefaultFilesOptions
                        {
                            DefaultFileNames = new[] { "Index.cshtml" }
                        });
                    });
        }
    }
    

    The UseDefaultFiles method is used to specify the default file names for a directory. In this case, it is set to Index.cshtml. You can replace this with the name of your default file.

    Note that this approach assumes that your Razor Pages are located in the wwwroot folder. If they are located elsewhere, you will need to adjust the UseWebRoot method accordingly.


    References:

    1 person found this answer helpful.
    0 comments No comments

  2. Bruce (SqlWork.com) 60,361 Reputation points
    2024-06-18T01:05:05.2266667+00:00

    Did you install the iis hosting module required to host .net core websites?

    https://video2.skills-academy.com/en-us/aspnet/core/host-and-deploy/iis/hosting-bundle?view=aspnetcore-8.0