| by Achyut Kendre | 37 comments

Built in Middleware’s in ASP.NET Core.

ASP.NET Core 3.1
Built in Middleware

Example Code: https://drive.google.com/file/d/1yxnw_0Urbg8a7EMSczlcS8-pNslkRYEM/view?usp=sharing

ASP.Net Core provides you various built in middleware, in this article we will have a look at few middleware’s.

ASP.NET Core is a modular framework. There are many middleware plug-ins available which can be used in our application.You can add the built in middle ware in request pipe line in same way, you just need to call the the method for that middle ware.
E.g. App.UseStaticFiles() in Configure method of startup class.

Few Built in Middleware’s are

MiddlewareDescription
AuthenticationAdds authentication support.
CORSConfigures Cross-Origin Resource Sharing.
RoutingAdds routing capabilities for MVC or web form
SessionAdds support for user session.
StaticFilesAdds support for serving static files and directory browsing.
DiagnosticsAdds support for reporting and handling exceptions and errors.
Built in Middleware’s

Static Files

By default as ASP.NETCore Application will not serve static files. The default directory for static files is wwwroot, it is built in folder for static files. To serve static files UseStaticFiles() middleware is required. You can create a sub folder under wwwroot for files like css, js etc.

To use the static file middleware call UseStaticFile() Method in configure method of startup class before every thing as follows –

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }

You can also create the sub folders under wwroot folder for css, js and images also.

Default File or Document Middleware

By default asp.net core does not show any default file. If you want to force ASP.NET core to force default file using Default Document Middleware.
Following are the default files –

  • Index.htm
  • Index.html,
  • Default.htm
  • Default.html

To use the default file/document middleware use the method UseDefaultFile() which will inject default file middleware in request pipe line and it will search for a file index.html, index.htm,default.htm, default.html, in wwroot if any of this file is available then it will be get loaded in browser as a default file. You can use default file /document middleware as follows –

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseDefaultFiles(dfo);// use dfo.
            app.UseStaticFiles();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }

You should remember default files middleware should be injected before StaticFiles middleware and by default it will search for index.htm, index.html, default.htm, default.html. if you want to modify it and make any other file as default document then you can use DefaultFilesOptions. You can modify the default document using default document options as follows –

DefaultFilesOptions options= newDefaultFilesOptions(); options.DefaultFileNames.Clear(); options.DefaultFileNames.Add("home.html");
app.UseDefaultFiles(options); 

What is Directory Browser Middleware in ASP.NET Core?

If you want to enable the directory browsing in your application we can use the middle ware. For this you can use the UseDictionaryBrower.

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

         app.UseDirectoryBrowser(); // show list of directories from app.

            app.UseRouting();
          

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }

Use File Server Middleware

All these three middle wares UseStaticFiles, UseDefaultDocument , UseDirectory browser combined into one middleware called UseFileServer Middleware. You can also define the options to this middle ware using UseFileServerOptions class.

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
             FileServerOptions fs = new FileServerOptions();
            fs.DefaultFilesOptions.DefaultFileNames.Clear();
            fs.DefaultFilesOptions.DefaultFileNames.Add("home.html");
            app.UseFileServer(fs);
            app.UseRouting();
             app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }

37 Comments

Kuliah Terbaik

December 17, 2023 at 3:13 pm Reply

Could I have your permission to copy your article? Your dedication and expertise are evident, and I’m truly grateful for your contributions. Can you visit my website too?? at Kampus Terbaik Thanks! ID : CMT-PM26TPIUDSM43L1CAW

subscribe

January 9, 2024 at 6:38 pm Reply

Is Female Pornstars As Vital As Everyone Says? subscribe

Kayleigh wanless.nude

January 10, 2024 at 3:42 am Reply

15 Of The Best Twitter Accounts To Discover Tattooed
Pornstars Kayleigh wanless.nude

najlepszy sklep

March 20, 2024 at 11:40 am Reply

Wow, marvelous blog structure! How long have you ever been running a blog for?
you make running a blog look easy. The whole glance of your site is
fantastic, let alone the content material! You can see similar here
sklep internetowy

najlepszy sklep

March 20, 2024 at 7:18 pm Reply

Wow, superb blog structure! How lengthy have you ever been blogging
for? you made running a blog glance easy. The entire glance
of your site is excellent, let alone the content material!
You can see similar here sklep online

najlepszy sklep

March 22, 2024 at 6:44 am Reply

I just could not depart your site before suggesting that I
actually enjoyed the standard info a person supply in your visitors?
Is going to be back incessantly to inspect new posts I saw similar here: E-commerce

sklep

March 23, 2024 at 4:38 am Reply

Wow, wonderful blog layout! How lengthy have you ever been running a
blog for? you made running a blog glance easy.
The whole glance of your web site is great, let alone the content material!
You can see similar here sklep online

sklep online

March 24, 2024 at 2:47 pm Reply

Hello! Do you know if they make any plugins to help with Search Engine Optimization? I’m trying to get
my blog to rank for some targeted keywords but I’m not seeing very good gains.
If you know of any please share. Appreciate it! You can read similar
art here: E-commerce

Analytics and social research

March 24, 2024 at 9:15 pm Reply

It’s very interesting! If you need help, look here: ARA Agency

sklep

March 26, 2024 at 11:49 pm Reply

Useful info. Fortunate me I found your website unintentionally, and I’m surprised why this accident didn’t happened in advance! I bookmarked it. expand your blogexpander

e-commerce

March 28, 2024 at 12:58 am Reply

Hello there! Do you know if they make any plugins to help
with Search Engine Optimization? I’m trying to get my blog to rank
for some targeted keywords but I’m not seeing very good results.
If you know of any please share. Thanks! You can read similar art here: Sklep online

e-commerce

March 28, 2024 at 2:24 pm Reply

Hi there! Do you know if they make any plugins to assist with
Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords but I’m
not seeing very good gains. If you know of any please share.
Thank you! You can read similar art here: Sklep online

sklep online

March 29, 2024 at 1:21 am Reply

Howdy! Do you know if they make any plugins to assist with
Search Engine Optimization? I’m trying to get my blog to rank
for some targeted keywords but I’m not seeing very good success.
If you know of any please share. Many thanks! You can read similar text here: Sklep online

ecommerce

March 29, 2024 at 12:36 pm Reply

Hey! Do you know if they make any plugins to help with SEO?

I’m trying to get my blog to rank for some targeted keywords but I’m not seeing very good gains.
If you know of any please share. Thanks! You can read similar text here: Najlepszy sklep

https://hitman.agency/category/kb

April 3, 2024 at 11:26 pm Reply

Good day! Do you know if they make any plugins to help with SEO?
I’m trying to get my site to rank for some targeted keywords but I’m
not seeing very good gains. If you know of any please share.

Thank you! I saw similar article here: Backlinks List

List of Backlinks

April 4, 2024 at 5:46 am Reply

Hello! Do you know if they make any plugins to help with SEO?
I’m trying to get my site to rank for some targeted keywords but I’m not seeing very good success.
If you know of any please share. Cheers! I saw similar art here:
GSA Verified List

GSA Verified List

April 4, 2024 at 5:31 pm Reply

Howdy! Do you know if they make any plugins to help with SEO?
I’m trying to get my site to rank for some targeted keywords but I’m not seeing very good success.

If you know of any please share. Many thanks!
You can read similar text here: Scrapebox List

GSA Verified List

April 6, 2024 at 1:55 am Reply

Good day! Do you know if they make any plugins
to help with SEO? I’m trying to get my site to rank for some targeted keywords but I’m not seeing very good results.
If you know of any please share. Cheers! You can read similar art here: List of Backlinks

pornstars on onlyfans

April 13, 2024 at 3:45 pm Reply

The Intermediate Guide Towards Best Onlyfans Pornstar pornstars on onlyfans

best

April 13, 2024 at 9:25 pm Reply

Five Things You Don’t Know About Rewrite Ai best

search engine marketing Agency

April 14, 2024 at 4:52 am Reply

11 Ways To Totally Block Your Search Engine Optimisation Agency search engine marketing Agency

Alphonse

April 14, 2024 at 7:38 am Reply

10 Things You Learned In Kindergarden Which Will Help You With SEO Optimization Near Me local seo expert near me
(Alphonse)

search engine optimisation package

April 14, 2024 at 11:45 am Reply

This Is The Advanced Guide To Search Engine Optimization Cost search engine optimisation package

Elliot

April 14, 2024 at 12:14 pm Reply

9 Lessons Your Parents Teach You About Best CSGO Opening Site counter-strike cases (Elliot)

Cs Go

April 14, 2024 at 1:51 pm Reply

The Most Hilarious Complaints We’ve Seen About Counter Strike New Cs Go

Case Revolution

April 14, 2024 at 2:40 pm Reply

20 Interesting Quotes About CSGO Best Betting Sites Case Revolution

https://www.hulkshare.com

April 14, 2024 at 2:50 pm Reply

5 Lessons You Can Learn From Case Battle CS GO case chroma – https://www.hulkshare.com

squirting Dildo sex toy

April 14, 2024 at 2:56 pm Reply

7 Simple Changes That’ll Make A Big Difference In Your Squirting Dildo Sex Toy
squirting Dildo sex toy

business

April 14, 2024 at 2:59 pm Reply

10 Things Everybody Hates About Affordable Search Engine Optimization business

Chroma 2 case

April 14, 2024 at 9:45 pm Reply

10 Things You Learned In Kindergarden That’ll
Help You With CSGO Cases Explained Chroma 2 case

seo services in london

April 15, 2024 at 7:41 am Reply

14 Common Misconceptions About Local SEO Service seo services in london

Brigette

April 15, 2024 at 8:10 am Reply

5 Reasons CS GO Weapon Case Is Actually A Great Thing operation bravo case,
Brigette,

http://www.fjclwz.Us/

April 15, 2024 at 11:34 am Reply

10 What CSGO Cases To Invest In Projects Related
To What CSGO Cases To Invest In To Extend Your Creativity operation hydra case,
http://www.fjclwz.Us/,

operation phoenix weapon case

April 15, 2024 at 1:00 pm Reply

The Most Significant Issue With CSGO Cases Highest Roi And How To Fix It operation phoenix weapon case

revolver case

April 15, 2024 at 7:13 pm Reply

One Case Opening Sites CSGO Success Story You’ll Never Imagine revolver case

Modesta

April 15, 2024 at 7:39 pm Reply

12 Companies Leading The Way In CSGO New Case clutch case (Modesta)

operation wildfire case

April 15, 2024 at 9:55 pm Reply

The Top 5 Reasons People Thrive In The CSGO Most Profitable
Cases Industry operation wildfire case

Leave a Reply