Static files and default files in .NET apps
Static files and default files in .NET apps

Static Files vs Default Files in ASP .NET Core Web Applications

When a new ASP .NET Core web application is created, it contains a default request processing pipeline. We have seen how static files middleware should be configured in last few posts.

There is another filter – UseDefaultFiles(). In this article, we are going to have a look at how this middleware works and how it should be configured.

What are default files ?

When an application is deployed, generally there might be a host name associated with it. The host name identifies the application which should handle the request.

What if a request comes to application and it does not specify any particular endpoint ?

e.g when an application runs on localhost with https://localhost:42000 url, it does not specify the page which should be rendered. In this case what should happen ?

Generally, web servers hosting the application can be configured to return a default page. For most of the web servers the default file can be any file with below name – depending on server configurations. It should be present at the root of web application.

  • default.htm
  • default.html
  • index.htm
  • index.html

These are generally referred to as default files. Web server serves the default files, without browser knowing it. Even though default file is served, the address bar in browser still unchanged showing only application’s host/domain name.

What does UseDefaultFiles do ?

UseDefaultFiles() essentially just rewrites the incoming request URL and UseStaticFiles() is supposed to return the file.

If no configuration options are passed, it tries to search the files in WWWROOT (more specifically, in web root directory). The files with below names are searched. First file found is served.

  • default.htm
  • default.html
  • index.htm
  • index.html

If you want any specific file to be searched instead of above mentioned files, then a file name (or a list of file names) can be passed to default files middleware as shown in the code example below.

Code

Below code shows usage of default files middleware.

I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply