How to find out if the current application is an ASP.NET web app

后端 未结 4 1117
Happy的楠姐
Happy的楠姐 2021-02-01 08:05

From a managed class library, I\'d like to find out whether the currently executing application is an ASP.NET web application (web forms or MVC) or not.

I have seen diff

4条回答
  •  长发绾君心
    2021-02-01 08:39

    First, I agree that the question is "wrong." Your library shouldn't care. Second, as below, there's a number of heuristics but ultimately no really good way to know. Your question doesn't make it clear why you want to know or a validation for that.

    That said, if it were me, I'd probably look at System.Web.HttpContext.Current if I cared about the current request, or System.Web.HttpRuntime.AppDomainAppId if I wanted a general check.

    NO GUARANTEES. This is likely a bad idea.

    From Stefan on the ASP.NET team:

    If it’s a web app, and its running managed code, 99.9999% likelihood its ASP.NET -). If he means something different such as looking at the IIS metabase and trying to figure out if an application is really an ASP.NET app or not – then some kind of heuristics would be needed. For example, for a given application in the IIS config/metbase, get the physical root of the application – then look and see if web.config, *.aspx, *.ashx, etc… exist in the folder, or any of the subfolders. If the answer is yes, then it is likely an ASP.NET application. Unfortunately it is not sufficient to make this determination just off of configuration data stored by IIS. Every application pool has an associated CLR version, even if no managed code ever runs in the application pool. And the second ASP.NET integration with IIS is enabled on a machine, the default module/handler lists all include ASP.NET entries.

    In the case of a library, the heuristic I described would be the way to go.

    In present times though, what’s “an ASP.NET application” anymore? These days I can stick classic .asp, ASP.NET, static HTML, and php all into the exact same vdir structure and have it all function semi-coherently as a single "application".

提交回复
热议问题