{"id":1756,"date":"2023-06-14T09:02:57","date_gmt":"2023-06-14T09:02:57","guid":{"rendered":"http:\/\/waqar-arshad.com\/?p=1756"},"modified":"2023-06-14T09:02:57","modified_gmt":"2023-06-14T09:02:57","slug":"asp-net-core-serving-static-files","status":"publish","type":"post","link":"http:\/\/waqar-arshad.com\/index.php\/2023\/06\/14\/asp-net-core-serving-static-files\/","title":{"rendered":"Asp.Net Core Serving Static Files"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1756\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">    <\/span>\r\n<\/div><div class=\"pld-dislike-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-dislike-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"1756\" data-trigger-type=\"dislike\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-down\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-dislike-count-wrap pld-count-wrap\"><\/span>\r\n<\/div><\/div>\n<h1 class=\"wp-block-heading\">ASP.NET Core &#8211; Serving Static Files<\/h1>\n\n\n\n<p>Here, we will learn how to serve static files such as html, JavaScript, CSS, or image files on HTTP request without any server-side processing.<\/p>\n\n\n\n<p>ASP.NET Core application cannot serve static files by default. We must include&nbsp;<code>Microsoft.AspNetCore.StaticFiles<\/code>&nbsp;middleware in the request pipeline.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install StaticFiles Middleware<\/h2>\n\n\n\n<p>The&nbsp;<strong>Microsoft.AspNetCore.StaticFiles<\/strong>&nbsp;middleware package is already included in the meta package&nbsp;<code>Microsoft.AspNetCore.All<\/code>, so we don&#8217;t need to install it separately in ASP.NET Core 2.x application.<\/p>\n\n\n\n<p>To install StaticFiles middleware in ASP.NET Core 1.x application, open NuGet package manager by right clicking on project in the solution explorer and select&nbsp;<strong>Manage NuGet Packages..<\/strong>. Search for staticfiles in the search box in the browse tab. This will display&nbsp;<strong>Microsoft.AspNetCore.StaticFiles<\/strong>&nbsp;middleware as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"359\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/dotnet-2.png\" alt=\"\" class=\"wp-image-1757\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/dotnet-2.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/dotnet-2-300x179.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/install-staticfiles-middleware.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Install StaticFiles Middleware<\/p>\n\n\n\n<p>Click on the&nbsp;<strong>Install<\/strong>&nbsp;button on the right pane to install it. Once installed, the&nbsp;<code>Microsoft.AspNetCore.StaticFiles<\/code>&nbsp;is automatically included in the dependencies section of the project.json.<\/p>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfiles.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>StaticFiles Dependency in project.json<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"492\" height=\"217\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture42.png\" alt=\"\" class=\"wp-image-1758\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture42.png 492w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture42-300x132.png 300w\" sizes=\"auto, (max-width: 492px) 100vw, 492px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Using StaticFiles Middleware<\/h2>\n\n\n\n<p>By default, all the static files of a web application should be located in the web root folder&nbsp;<strong>wwwroot<\/strong>. To understand this, let&#8217;s create a simple default.html in the wwwroot folder with the following content.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"312\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture43.png\" alt=\"\" class=\"wp-image-1759\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture43.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture43-300x155.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfile-html2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Default.html<\/p>\n\n\n\n<p>Now, to serve the above Default.html static file, we must add StaticFiles middleware in the&nbsp;<code>Configure()<\/code>&nbsp;method of Startup file as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>class<code> <\/code>Startup<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>public<code> Startup()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; } <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;<\/code>public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseStaticFiles();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.Run(<\/code>async<code> (context) =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>await<code> context.Response.WriteAsync(<\/code>\"Hello World\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>As you can see above, the&nbsp;<code>app.UseStaticFiles()<\/code>&nbsp;method adds StaticFiles middleware into the request pipeline. The&nbsp;<code>UseStaticFiles<\/code>&nbsp;is an extension method included in the StaticFiles middleware so that we can easily configure it.<\/p>\n\n\n\n<p>Now, open the browser and send http request&nbsp;<em>http:\/\/localhost:&lt;port&gt;\/default.html<\/em>&nbsp;which will display default.html as a response as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"389\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture44.png\" alt=\"\" class=\"wp-image-1760\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture44.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture44-300x194.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfiles2.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Serving HTML File<\/p>\n\n\n\n<p>This way we can serve any other file stored in wwwroot folder or sub-folder. For example, consider the following test.js file in the wwwroot folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"312\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture45.png\" alt=\"\" class=\"wp-image-1761\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture45.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture45-300x155.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfile-jscript.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>test.js<\/p>\n\n\n\n<p>Now, we can access this file by sending http:\/\/localhost:&lt;port&gt;\/test.js request.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"389\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture46.png\" alt=\"\" class=\"wp-image-1762\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture46.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture46-300x194.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfiles3.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Serving JS File<\/p>\n\n\n\n<p>Suppose, you want to serve files from the outside of web root folder (wwwroot). For example, you include images in the following Images folder as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"305\" height=\"464\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture47.png\" alt=\"\" class=\"wp-image-1763\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture47.png 305w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture47-197x300.png 197w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfiles7.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Serving Static Files<\/p>\n\n\n\n<p>Now, specify&nbsp;<code>StaticFileOptions<\/code>&nbsp;parameter in the&nbsp;<code>UseStaticFiles<\/code>&nbsp;method to serve images from the Images folder as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; app.UseStaticFiles(); <\/code>\/\/ For the wwwroot folder<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; app.UseStaticFiles(<\/code>new<code> <\/code>StaticFileOptions<code>()<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>FileProvider<code> = <\/code>new<code> <\/code>PhysicalFileProvider<code>(<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Path.Combine(<\/code>Directory<code>.GetCurrentDirectory(), <\/code>@\"Images\"<code>)),<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RequestPath = <\/code>new<code> <\/code>PathString<code>(<\/code>\"\/app-images\"<code>)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>As you can see, we used&nbsp;<code>FileProvider<\/code>&nbsp;option to specify Images folder from which static files will be served. The RequestPath option specifies the relative path in the URL which maps to the static folder.<\/p>\n\n\n\n<p>Now, a request to&nbsp;<em>http:\/\/localhost\/app-images\/MyImage.png<\/em>&nbsp;will serve the MyImage.png file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Set Default File<\/h2>\n\n\n\n<p>As we have seen above, default.html or test.js was served on the specific request for it. However, what if we want to serve default html file on the root request?<\/p>\n\n\n\n<p>Currently, when you send&nbsp;<em>http:\/\/localhost:&lt;port&gt;<\/em>&nbsp;request, it will be handled by run method and display the following result.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"389\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture48.png\" alt=\"\" class=\"wp-image-1764\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture48.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture48-300x194.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfiles5.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<p>To serve default.html on the root request&nbsp;<em>http:\/\/localhost:&lt;port&gt;<\/em>, call&nbsp;<code>UseDefaultFiles()<\/code>&nbsp;method before&nbsp;<code>UseStaticFiles()<\/code>&nbsp;in the&nbsp;<code>Configure<\/code>&nbsp;method as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseDefaultFiles();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseStaticFiles();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; app.Run(<\/code>async<code> (context) =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>await<code> context.Response.WriteAsync(<\/code>\"Hello World\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>The&nbsp;<code>UseDefaultFiles<\/code>&nbsp;configures the DefaultFiles middleware which is a part of StaticFiles middleware. This will automatically serve html file named default.html, default.htm, index.html or index.htm on the http request&nbsp;<em>http:\/\/localhost:&lt;port&gt;<\/em>. The above example will display default.html file on&nbsp;<em>http:\/\/localhost:&lt;port&gt;<\/em>&nbsp;as shown below.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"602\" height=\"389\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture49.png\" alt=\"\" class=\"wp-image-1765\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture49.png 602w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture49-300x194.png 300w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/staticfiles6.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a>Serving Static Files<\/p>\n\n\n\n<p>&nbsp;Note:<\/p>\n\n\n\n<p>Order of middleware is very important.&nbsp;<code>app.UseDefaultFiles()<\/code>&nbsp;should be added before&nbsp;<code>app.UseStaticFiles()<\/code>&nbsp;in the request pipeline.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FileServer<\/h2>\n\n\n\n<p>The FileServer middleware combines the functionalities of UseDefaultFiles and UseStaticFiles middlware. So, instead of using both the middlware, just use UseFileServer in the Configure method.<\/p>\n\n\n\n<p>UseFileServer = UseDefaultFiles + UseStaticFiles<\/p>\n\n\n\n<p>Example: UseFileServer<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>void<code> Configure(<\/code>IApplicationBuilder<code> app, <\/code>IHostingEnvironment<code> env)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; app.UseFileServer();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; app.Run(<\/code>async<code> (context) =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/code>await<code> context.Response.WriteAsync(<\/code>\"Hello World\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Serve static files from different folder than wwwroot folder in ASP.NET Core<\/h1>\n\n\n\n<p>You can configure middleware to serve static files from other folders along with default web root folder wwwroot.<\/p>\n\n\n\n<p>For example, we will server admin.html from the following admin folder and also test.html from wwwroot folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"273\" height=\"332\" src=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture50.png\" alt=\"\" class=\"wp-image-1766\" srcset=\"http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture50.png 273w, http:\/\/waqar-arshad.com\/wp-content\/uploads\/2023\/06\/Picture50-247x300.png 247w\" sizes=\"auto, (max-width: 273px) 100vw, 273px\" \/><\/figure>\n\n\n\n<p><a href=\"https:\/\/www.tutorialsteacher.com\/Content\/images\/core\/admin.png\" target=\"_blank\" rel=\"noreferrer noopener\"><\/a><\/p>\n\n\n\n<p>Now, configure the StaticFiles middleware in the Configure() method of Startup class as shown below.<\/p>\n\n\n\n<p>Example: Configure StaticFiles Middleware<\/p>\n\n\n\n<p>&nbsp;Copy<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public<code> <\/code>class<code> Startup<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>{<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>\/\/ This method gets called by the runtime. Use this method to configure the HTTP request pipeline.<code><\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; <\/code>public<code> <\/code>void<code> Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseStaticFiles();<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.UseStaticFiles(<\/code>new<code> StaticFileOptions() {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FileProvider =&nbsp; <\/code>new<code> PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), <\/code>\"Content\"<code>)),<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RequestPath = <\/code>new<code> PathString(<\/code>\"\/Admin\"<code>)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; app.Run(async (context) =&gt;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; await context.Response.WriteAsync(<\/code>\"Hello World!\"<code>);<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; });<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&nbsp;&nbsp;&nbsp; }<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>}<\/code><\/pre>\n\n\n\n<p>As you can see,&nbsp;<code>app.UseStaticFiles()<\/code>&nbsp;enables default web root folder wwwroot to serve the static files.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>app.UseStaticFiles(<\/code>new<code> StaticFileOptions() {<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; FileProvider =&nbsp; <\/code>new<code> PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), <\/code>\"admin\"<code>)),<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>&nbsp;&nbsp;&nbsp; RequestPath = <\/code>new<code> PathString(<\/code>\"\/admin\"<code>)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>});<\/code><\/pre>\n\n\n\n<p>The above code configures Content admin folder to serve static files on the request path \/admin. So now, we will be able to execute HTTP request http:\/\/localhost:1234\/admin\/admin.html to display static admin.html page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ASP.NET Core &#8211; Serving Static Files Here, we will learn how to serve static files such as html, JavaScript, CSS, or image files on HTTP request without any server-side processing. ASP.NET Core application cannot serve static files by default. We must include&nbsp;Microsoft.AspNetCore.StaticFiles&nbsp;middleware in the request pipeline. Install StaticFiles Middleware The&nbsp;Microsoft.AspNetCore.StaticFiles&nbsp;middleware package is already included in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[13],"tags":[33,36],"class_list":["post-1756","post","type-post","status-publish","format-standard","hentry","category-asp-net-core","tag-net","tag-asp-net"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"admin","author_link":"http:\/\/waqar-arshad.com\/index.php\/author\/waqar_29_1\/"},"uagb_comment_info":24,"uagb_excerpt":"ASP.NET Core &#8211; Serving Static Files Here, we will learn how to serve static files such as html, JavaScript, CSS, or image files on HTTP request without any server-side processing. ASP.NET Core application cannot serve static files by default. We must include&nbsp;Microsoft.AspNetCore.StaticFiles&nbsp;middleware in the request pipeline. Install StaticFiles Middleware The&nbsp;Microsoft.AspNetCore.StaticFiles&nbsp;middleware package is already included in&hellip;","_links":{"self":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1756","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/comments?post=1756"}],"version-history":[{"count":1,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1756\/revisions"}],"predecessor-version":[{"id":1767,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/posts\/1756\/revisions\/1767"}],"wp:attachment":[{"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/media?parent=1756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/categories?post=1756"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/waqar-arshad.com\/index.php\/wp-json\/wp\/v2\/tags?post=1756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}