2
votes

I have an ASP.NET WebForms application which requires .NET Framework 4.8 (or higher). I want the application to "fail early" if the required framework version is unavailable (e.g. on a system with only 4.7 installed). In other words, I want something like the supportedRuntime tag for executables, just for web applications.

Can this be achieved via some web.config setting? I have tried:

  • system.web/httpRuntime/targetFramework
  • system.web/compilation/targetFramework
  • startup/supportedRuntime/sku (just for completeness, I didn't expect it to work)

I.e.:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  ...
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
  </startup>

  <system.web>
    <httpRuntime targetFramework="4.8" />
    <compilation targetFramework="4.8" />
    ...
  </system.web>
</configuration>

However, the web application still happily runs on a server with only .NET Framework 4.7 installed.

Did I miss anything or is this feature just not available?

2
Just out of curiosity, how this "fail early" should look like for a web app? Return 500 for every request?Wiktor Zychla
@WiktorZychla: Yes, return 500 (with a descriptive and helpful error message, if customErrors/mode allows) would be great.Heinzi
A simple runtime check in Application_Start should fail pretty early (you could even pull the version to check against from web.config) stackoverflow.com/questions/951856Dave M
@DaveM: Yes, that was my backup plan. I just wanted to make sure that I didn't reinvent the wheel first.Heinzi

2 Answers

1
votes

As far as I know, if you don't install the .net framework 4.8, if you run yur web application on IIS, it will show below error:

enter image description here

I suggest you could try to follow below steps to check you have installed the .net frameworr 4.8:

1.From the Start menu, choose Run, enter regedit, and then select OK.You must have administrative credentials to run regedit.

2.In the Registry Editor, open the following subkey: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full. If the Full subkey isn't present, then you don't have the .NET Framework 4.5 or later installed.

3.Check for a DWORD entry named Release. If it exists, then you have .NET Framework 4.5 or later versions installed. Its value is a release key that corresponds to a particular version of the .NET Framework. In the following figure, for example, the value of the Release entry is 528040, which is the release key for .NET Framework 4.8.

Result:

enter image description here

0
votes

@BrandoZhang's answer is correct, <compilation targetFramework="4.8" /> does work. I added this answer to explain why it didn't work in my case. (Too long for a comment, and it might help someone with the same problem.)

In my case, web.config wasn't located in the web application's directory. It was located in the parent directory, because I have multiple web applications that need to be configured in exactly the same way and I did not want to repeat myself.

It turns out that, apparently, compilation's targetFramework attribute does not get inherited by child applications (if you know why, feel free to leave a comment). Adding the following web.config file directly in the application's directory fixed the issue for me:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.web>
    <compilation targetFramework="4.8" />
  </system.web>
</configuration>