Back to all posts

Using a Web.config File With ASP.NET Core

Posted on Mar 18, 2020

Posted in category:
Development
.NET

One of the most significant changes for those that transitioned from ASP.NET to ASP.NET Core was the transition of configuration information from the web.config file to the appsettings.json file. However, for those of us still deploying our applications to IIS, there are many reasons that we might want to include a web.config with our apps.

Why Is It Needed?

Given that IIS can infer configuration data from a web.config file, you might want to include one in your project to help configure the way that IIS responds to various requests. A few examples of things you might need to use this for.

  • Configuration of Compression
  • Custom Mime Type Mappings
  • Removal of IIS Added Headers

I'm sure there are more examples; however, these are the most common ones that I have experienced.

Adding the File

The process of adding the file to the project is as simple as you might think. Right-click on your project and select "Add New Item" and select Configuration File from the listing. This will add blank web.config file to your project.

The key is to now view the properties for the file and set the Copy to Output Directory property to Copy if Newer. By doing this the file will now be included when you run the application as well as when you publish the application.

Sample File

As an illustrative sample, this website uses a custom web.config file to enable IIS compression for SVG images, which by default is not included in the listing of static file types.

Sample Web.config Customizations
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <urlCompression doDynamicCompression="true" doStaticCompression="true"/>
    <httpCompression>
      <staticTypes>
        <add mimeType="image/svg+xml" enabled="true"/>
      </staticTypes>
    </httpCompression>
  </system.webServer>
</configuration>

As you can see here we are simply configuring the webServer only. This will have no impact on anything else, it just controls how IIS responds to the items served by IIS. You can still do all of your custom work inside of your application, providing a great bridge to allow default functionality to benefit you.

Summary

As much as we all want to see the web.config disappear, there is still a great use for it. I hope that you find this helpful in your project ventures.