Tech Junkie Blog - Real World Tutorials, Happy Coding!: Hour 2: Enable ASP.NET Core to Serve Static Files

Wednesday, July 24, 2019

Hour 2: Enable ASP.NET Core to Serve Static Files

As I have mentioned before ASP.NET Core decouples the application from the infrastructure as much as possible.  Therefore, you have to tell it exactly what you want in your project.  In this blog post we are going to tell ASP.NET that we want to serve static html files in our application.

Here are the steps to serve static files in our ASP.NET Core application.

1.  Open the "NorthwindCafe.Web" project, then click on the "Startup.cs" file in the project.  You will see the following markup in the Configure method

        public void Configure(IApplicationBuilder app)
        {
            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }

2.  Go into the Configure method, remove the existing code and type in the following code
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseDefaultFiles();
            app.UseStaticFiles();
        }



The code above tells ASP.NET Core to use static files and also use the default files in the web server.  Therefore, it will look for the index.html file in the wwwroot folder.  So create an index.html file in the wwwroot folder and type in the following markup.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <h1>This a static file</h1>
</body>
</html>










github Repository: https://github.com/techjunkiejh/NorthwindCafe/tree/Hour2


2 comments:


  1. Günümüzde eğlence ve bilgi alanında çeşitli alternatifler ortaya çıkarken, kullanıcıların ihtiyaçlarına uygun içeriklere ulaşması oldukça önemli hale geldi. Bu noktada, farklı platformlar üzerinden sunulan hizmetler sayesinde, herkes kendine uygun bir deneyim bulabilir. Özellikle görüntülü show gibi seçenekler, izleyicilere daha etkileşimli ve görsel açıdan zengin içerikler sunma imkanı tanıyor. Bu sayede, kullanıcılar hem eğleniyor hem de bilgi edinebiliyor. Teknolojinin gelişimiyle birlikte, bu tarz içeriklerin önemi giderek artıyor.

    ReplyDelete

Search This Blog