What do the TargetFramework settings mean in web.config in ASP .NET MVC?

Solution 1:

The reason of targetFramework existence in web.config is to keep compatibility issues out between breaking changes for each version of .NET Framework. The difference between targetFramework on compilation and httpRuntime belongs to each development and deployment environment.

According to MSDN blog:

<compilation targetFramework="4.6" />

Selects which version of the .NET Framework’s reference assemblies are used when performing compilation. (Note: Visual Studio requires that this element be present in Web.config, even though we auto-infer it.)

This element determines the assembly version used during compilation to create dependencies and related assemblies from current project.

<httpRuntime targetFramework="4.5" /> means that current project designed to use .NET 4.5 runtime assemblies without recompiling existing project assemblies in deployment machine before loading it into memory.

Hence, we can conclude that version number defined on targetFramework in httpRuntime element designed to maintain compatibility between compiled project and available assemblies in runtime usage, depending on which version of runtime files being used in target machine.

Thus, in your case, this is not a wrong behavior, the project creator(s) simply want to keep runtime compatibility to lowest runtime version available in target machine with similar characteristics (i.e. version 4.5), even the project compiled with newer version of .NET assemblies. The difference between version 4.5 and 4.6 is relatively small, thus keeping runtime version down to 4.5 still acceptable on this context.

Related references:

Solution 2:

As per my understanding <compilation debug="true" targetFramework="4.6" /> is being suppressed by <httpRuntime targetFramework="4.5" /> since httpRuntime gets translated to following

<compilation targetFramework="4.5" />
<machineKey compatibilityMode="Framework45" />
<pages controlRenderingCompatibilityVersion="4.5" />

So above setting is probably due to some misunderstanding or a bug if done by VS directly which I don't believe is true.

To understand how this setting and all other related stuff means this blog titled All about <httpRuntime targetFramework> written by a Microsoft employee might help you. But the gist of it is;

The .NET Framework (including ASP.NET) strives to maintain near-100% compatibility when an existing framework is updated on a machine. We try to ensure as much as possible that if an application was developed and deployed against .NET Framework 4, it will just continue to work on 4.5. This normally means keeping quirky, buggy, or undesirable behaviors in the product between versions, as fixing them may negatively affect applications which were relying on those behaviors.