Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand the new behavior of appsettings.json vs web.config in .NET Core but confused by contradictory information on MSDN

Tags:

.net

So, I'm reading through all of the .NET Core Fundamentals articles on MSDN while hacking around on a new .NET Core MVC application in Visual Studio 2017. There seem to be some inconsistencies between what I'm reading in the articles and what I'm seeing in my application. I was hoping somebody could help me understand.

So, I understand that a new .NET Core project created in Visual Studio, by default, is configured to use the Kestrel web server with an IIS Express web server acting as a reverse proxy when running in development.

I also understand that the ASP.NET Core Module hooks into the IIS pipeline and, among other things, redirects traffic to your .NET Core web application.

Here's what I'm getting hung up on.

From the MSDN article on .NET Core Configuration:

The web.config file A web.config file is required when you host the app in IIS >or IIS-Express. web.config turns on the AspNetCoreModule in IIS to launch your >app. Settings in web.config enable >the AspNetCoreModule in IIS to launch your >app and configure other IIS settings and modules. If you are using Visual >Studio and delete web.config, Visual Studio will create a new one.

I was under the impression that in .NET Core, application configuration had been moved out of the web.config file, and was instead controlled through a number of different mechanisms, one of which is the appsettings.json file. In fact, creating a new .NET Core MVC application in Visual Studio doesn't even create a web.config file in my solution directory.

But from the article quoted above, it sounds like the ASP.NET Core Module is still configured through a web.config file? What's confusing is that the article says Visual Studio will create a web.config file for me if one doesn't exist, but I've run the application a few times and don't see a web.config file created anywhere.

What confuses me further is more seemingly contradictory information from the MSDN ASP.NET Core module reference article:

The ASP.NET Core Module is configured via a site or application web.config file >and has its own aspNetCore configuration section within system.webServer. >Here's an example web.config file that the Microsoft.NET.Sdk.Web SDK will >provide when the project is published for a framework-dependent deployment with >placeholders for the processPath and arguments:

So wait - this says that the act of publishing my application is what creates the web.config file.

So is it only full blown IIS that needs a web.config file? Maybe IIS Express and IIS both work with default behavior if no web.config is provided, but I need a web.config file if I want to override the default behavior?

Does anybody have a solid understanding of how this all works in .NET Core that is willing to set me straight?

like image 887
Bloog Avatar asked Sep 26 '17 04:09

Bloog


1 Answers

Bloog, you have probably already found your answers by now - but I wanted to comment here just in case and for anyone else looking into this.

web.config from what I understood was no longer part of a ASP.net Core web application either. And, like you said, when creating new ASP.net Core Web Application projects - there is no web.config file in my projects either. But, when I Publish me apps to my IIS based web host - there is a web.config file in the root of the web app folder on the host system.

After much research, it seems to me that this is not a 'web.config' file in the same sense - it is being used as a configuration file for the environment 'wrapper' that the kestrel server is running in. I have found multiple explanations to configure database connection strings in the < aspNetCore > section of this file under the < system.webserver > section. I was also using IIS Manager for Remote Administration to configure IIS settings on my hosting service. One setting, in particular, was to us IP Restrictions to stop users from anywhere but my IP from accessing my test site. I would configure this and then test it and it was working great, but then when I would make some changes and re-Publish my site - this setting would go back to an unconfigured state.

Well, I reconfigured it again and then went to set up some DB connection strings in the web.config file, but I opened it, I saw that there was a section added to the web.config file with my IIS IP Restriction settings there as well! HMMM!

So, I re-published the site again and then re-checked the web.config file again... no IP Restriction settings! So IIS configurations were directly modifying this web.config file. Again, I re-configured my IP Restrictions, then copied this web.config file and added it to my Visual Studio project. Now when I re-Publish - this file is Published as well and its keeping my IP Restrictions in place.

When you look at the structure of this web.config file, it seems like the only thing in it is related to the IIS environment, so I think it is in effect acting like an IIS config files and helping to create almost a simple virtual server environment for the kestrel server to run in. In my case, I am hosting on a shared hosting server, so I cannot access my own environment variables where everyone suggests putting DB connection strings for Core projects - but this web.config file seems to be taking these environment variables and IIS settings and applying them to the wrapper that kestrel is running in.

I can't say what all it can do - especially in comparison to its original function in ASP.NET projects - but I think - it seems to have nearly nothing in common to do with it. It would suit me fine if they would call it environment.config or server.config or... This has been very confusing conflating the two iterations of this web.config file like this.

I am still testing with this - but it is affecting IIS Express from Visual Studio. Currently, I'm getting "Unable to start process C:\Program Files\dotnet\dotnet.exe. The web server request failed with status code 500, Internal Server Error. The full response has been written to C:\Users\mywindowsuseracct\AppDate\Local\Temp\HTTPFailure_12-52-15.html" when I start my project - but, If I simply change the name of the web.config file in the VS project, then it runs fine. I'd say it's because this web.config is telling IIS where to run the aspNetCore project dll and it is currently based on the location of the file on the host server vs VS IIS Express.

So I think the next step will be trying to see how to only publish this file in Release mode when published to the host - and to be ignored when running in IIS Express in Dev mode.

Hope this helps!

like image 111
AMeador1 Avatar answered Oct 19 '22 03:10

AMeador1