Determining if ASP.Net is properly registered

偶尔善良 提交于 2019-11-28 05:20:43

First I would try running aspnet_regiis -lv. This should give you an output like:

1.1.4322.0      Valid           C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll
2.0.50727.0     Valid           c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll

that you can easily parse to verify that your target version is installed and valid. If it is not, you'll have to go the aspnet_regiis -i route.

Also, given that you can do this check in C#, you could add a test page to your ASP.NET application. After what you would normally consider a successful installation, do a HttpWebRequest on that test page. The page itself can be as simple as an empty page and as complicated as running a self-check of the installation (file/folder permissions, DB configuration, etc.) and would only return a HTTP 200 if everything is ok. Any timeout or error indicates a bad install. Then,optionally, delete the test page.

This snippet works for IIS7+

using Microsoft.Web.Administration;   

private static string[] ARR_STR_SUPPORTED_APP_POOLS = 
                         { "ASP.NET v4.0", "ASP.NET v4.5", ".NET v4.5", ".NET v4.0" };

public static ApplicationPool GetFirstSupportedAppPoolInstalled(this ServerManager mgr, IEnumerable<string> supportedAppPools)
{
    ApplicationPool result = null;
    foreach (string appPoolName in supportedAppPools)
    {
        result = mgr.ApplicationPools[appPoolName];
        if (result != null)
            break;
    }
    return result;
}

...
using (var mgr = new ServerManager())
{
   if (!mgr.IISAccessCheck())
      throw new ApplicationException("Error trying to access IIS 7!");

   ApplicationPool appPool = mgr.GetFirstSupportedAppPoolInstalled(ARR_STR_SUPPORTED_APP_POOLS);
   if (appPool == null)
       throw new ApplicationException("No appropriate .NET application pool found!");

   // you can do something with the app pool, if needed
}
...

You can adjust it as you want.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!