Configuring Startup Page In ASP.NET Core

Introduction
 
I think we all are familiar with the configuration of the default startup page in the previous versions of AP.NET but it’s slightly different in ASP.NET Core applications. In this article, I will explain how to configure the default startup page In ASP.NET Core. 
 
Default Startup Page Configuration
 
There are two ways to implement the default startup page in ASP.NET Core.
  • Default Configuration
  • Customized Configuration 
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
Default Configuration
 
We can use UseDefaultFiles() extension method in ASP.NET Core 1.0. UseDefaultFiles() will only search for the files given in "wwwroot". If any of the files are detected first in "wwwroot", the files are run as default in the client browser.
  • default.html
  • default.htm
  • index.html
  • index.htm
UseDefaultFiles must be called before UseStaticFiles or any other method (app.Run, app.Use) to serve the default file in the client-side browser. As you state UseStaticFiles() method after UseDefaultFiles(), it will run UseStaticFiles() method as a default and automatically terminates the other files which come after UseStaticFiles() method.
 
Customized Configuration
 
In this case, we are calling other customized pages as default startup pages in ASP.NET Core 1.0. Thus, we can use DefaultFilesOptions in ASP.NET Core 1.0. If you want to run other files as default, check the code given below in Startup.cs.
 
Full Code
 
The following code containsthe full source code of startup page configuration in ASP.NET Core.
  1. using Microsoft.AspNetCore.Builder;  
  2. using Microsoft.AspNetCore.Hosting;  
  3. using Microsoft.AspNetCore.Http;  
  4. using Microsoft.Extensions.DependencyInjection;  
  5. using Microsoft.Extensions.Logging;  
  6.    
  7. namespace StartupConfig  
  8. {  
  9.     public class Startup  
  10.     {  
  11.         // This method gets called by the runtime. Use this method to add services to the container.  
  12.         // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 Jump  
  13.         public void ConfigureServices(IServiceCollection services)  
  14.         {  
  15.         }  
  16.    
  17.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  18.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  19.         {  
  20.             loggerFactory.AddConsole();  
  21.                
  22.             DefaultFilesOptions DefaultFile = new DefaultFilesOptions();  
  23.             DefaultFile.DefaultFileNames.Clear();  
  24.             DefaultFile.DefaultFileNames.Add("Welcome.html");  
  25.             app.UseDefaultFiles(DefaultFile);  
  26.             app.UseStaticFiles();  
  27.    
  28.             if (env.IsDevelopment())  
  29.             {  
  30.                 app.UseDeveloperExceptionPage();  
  31.             }  
  32.    
  33.             app.Run(async (context) =>  
  34.             {  
  35.                 await context.Response.WriteAsync("Hello World!");  
  36.             });  
  37.         }  
  38.     }  
  39. }  
project.json
 
The versions will be change based on the latest version's updation in ASP.NET Core.
  1. {  
  2.   "dependencies": {  
  3.     "Microsoft.NETCore.App": {  
  4.       "version""1.0.1",  
  5.       "type""platform"  
  6.     },  
  7.     "Microsoft.AspNetCore.Diagnostics""1.0.0",  
  8.     "Microsoft.AspNetCore.Server.IISIntegration""1.0.0",  
  9.     "Microsoft.AspNetCore.Server.Kestrel""1.0.1",  
  10.     "Microsoft.Extensions.Logging.Console""1.0.0",  
  11.     "Microsoft.AspNetCore.StaticFiles""1.1.1"  
  12.   },  
  13.    
  14.   "tools": {  
  15.     "Microsoft.AspNetCore.Server.IISIntegration.Tools""1.0.0-preview2-final"  
  16.   },  
  17.    
  18.   "frameworks": {  
  19.     "netcoreapp1.0": {  
  20.       "imports": [  
  21.         "dotnet5.6",  
  22.         "portable-net45+win8"  
  23.       ]  
  24.     }  
  25.   },  
  26.    
  27.   "buildOptions": {  
  28.     "emitEntryPoint"true,  
  29.     "preserveCompilationContext"true  
  30.   },  
  31.    
  32.   "runtimeOptions": {  
  33.     "configProperties": {  
  34.       "System.GC.Server"true  
  35.     }  
  36.   },  
  37.    
  38.   "publishOptions": {  
  39.     "include": [  
  40.       "wwwroot",  
  41.       "web.config"  
  42.     ]  
  43.   },  
  44.    
  45.   "scripts": {  
  46.     "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]  
  47.   }  
  48. }  
Configuration Code
  1. DefaultFilesOptions DefaultFile = new DefaultFilesOptions();  
  2. DefaultFile.DefaultFileNames.Clear();  
  3. DefaultFile.DefaultFileNames.Add("Welcome.html");  
  4. app.UseDefaultFiles(DefaultFile);  
  5. app.UseStaticFiles();  
Reference
Downloads
 
You can download ASP.NET Core source code from the MSDN Code using the links mentioned below.