[英]How to find out if the current application is an ASP.NET web app
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.从托管 class 库中,我想了解当前执行的应用程序是否是 ASP.NET web 应用程序(web ZAC68B64VC 或不是368ACCEFE2)。
I have seen different approaches to do so, eg by checking one of the following:我已经看到了不同的方法来做到这一点,例如通过检查以下之一:
System.Web.Hosting.HostingEnvironment.IsHosted == true
System.Web.HttpContext.Current != null
System.Web.HttpRuntime.AppDomainAppId != null
System.Web.HttpRuntime.Cache != null
web.config
file (note: I don't think this is reliable)web.config
文件(注意:我认为这不可靠) The question is which approach should I be using?问题是我应该使用哪种方法? Are some of them invalid (ie might they return true even when running in a windows app) or are all of them equal?
其中一些是无效的(即,即使在 windows 应用程序中运行,它们也可能返回 true)还是它们都相等?
Update / clarification (sorry if my question wasn't clear enough):更新/澄清(对不起,如果我的问题不够清楚):
Note : I know I could implement a different solution (eg see comments below or Tomas Lycken's answer), but that is not the point of this question.注意:我知道我可以实施不同的解决方案(例如,请参阅下面的评论或 Tomas Lycken 的回答),但这不是这个问题的重点。 The class library is already existing, and I'd like to change as little code as possible!
class 库已经存在,我想更改尽可能少的代码!
Potentially Not reliable.可能不可靠。 MSDN implies that this will always return a new object if one does not exist already.
MSDN 暗示这将始终返回一个新的 object 如果一个尚不存在。 Thus there are technically times where you can call it and have one not exist before it is called.
因此,从技术上讲,有时您可以调用它并且在调用它之前不存在。
System.Web.Hosting.HostingEnvironment.IsHosted == true
All web environments need a context.所有 web 环境都需要上下文。 What handler is inside that context is what tells you the type of web environment.
该上下文中的处理程序告诉您 web 环境的类型。 (MvcHandler for example).
(例如 MvcHandler )。 Note this can be different types of handlers for the same environment - You can run MVC and web forms together for example.
请注意,对于同一环境,这可能是不同类型的处理程序 - 例如,您可以一起运行 MVC 和 web forms。 It just depends on what is currently being served and the pipeline it is using.
它仅取决于当前提供的服务以及它使用的管道。
System.Web.HttpContext.Current != null
All web apps need an application ID .所有 web 应用程序都需要一个应用程序 ID 。 It is unique and does not change as application pools are restarted.
它是唯一的,不会随着应用程序池的重新启动而改变。
System.Web.HttpRuntime.AppDomainAppId != null
I've never seen this, though just logically I can imagine a time where the cache is not used and thus wouldn't be reliable.我从未见过这种情况,尽管从逻辑上讲,我可以想象没有使用缓存因此不可靠的时间。
System.Web.HttpRuntime.Cache != null
You're right.你是对的。
checking for a web.config file (note: I don't think this is reliable)
检查 web.config 文件(注意:我认为这不可靠)
I use something of this sort within a library.我在图书馆中使用这种东西。 I've found it reliable.
我发现它可靠。
Page page = (HttpContext.Current != null && HttpContext.Current.Handler != null) ? HttpContext.Current.Handler as Page : null;
if (HttpRuntime.AppDomainAppId != null && page != null)
{
//I'm a web forms application
}
else if (HttpRuntime.AppDomainAppId != null && page == null && HttpContext.Current != null) { throw new InvalidOperationException("I'm an MVC application"); }
else throw new InvalidOperationException("Im not ASP.Net web");
I have to question your goal here: Why should the library be aware of what kind of application it is running from?我必须在这里质疑您的目标:为什么库应该知道它运行的是哪种应用程序?
To me, it sounds like you need to split the relevant part of your library in two parts - one for use with web apps, and one for use with winforms apps.对我来说,听起来您需要将库的相关部分分成两部分 - 一部分用于 web 应用程序,另一部分用于 winforms 应用程序。 (And possibly a third part, with everything that can be used by both types of apps...)
(可能还有第三部分,包含两种类型的应用程序都可以使用的所有内容......)
This might be helpful:这可能会有所帮助:
How can I detect if my .NET assembly is running from web site or from a desktop machine? 如何检测我的 .NET 程序集是从 web 站点还是从台式机运行?
if(HttpRuntime.AppDomainAppId != null)
{
//is web app
}
else
{
//is windows app
}
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.也就是说,如果是我,如果我关心当前请求,我可能会查看 System.Web.HttpContext.Current,或者如果我想要进行常规检查,我可能会查看 System.Web.HttpRuntime.AppDomainAppId。
NO GUARANTEES.没有保证。 This is likely a bad idea.
这可能是个坏主意。
From Stefan on the ASP.NET team:来自 ASP.NET 团队的 Stefan:
If it's a web app, and its running managed code, 99.9999% likelihood its ASP.NET -).
如果它是 web 应用程序及其正在运行的托管代码,则其 ASP.NET -) 的可能性为 99.9999%。 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.
如果他的意思有所不同,例如查看 IIS 元数据库并试图确定应用程序是否真的是 ASP.NET 应用程序 - 那么就需要某种启发式方法。 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.
例如,对于 IIS config/metbase 中的给定应用程序,获取应用程序的物理根目录 - 然后查看 web.config、*.aspx、*.ashx 等是否存在于该文件夹或任何子文件夹。 If the answer is yes, then it is likely an ASP.NET application.
如果答案是肯定的,那么很可能是 ASP.NET 应用程序。 Unfortunately it is not sufficient to make this determination just off of configuration data stored by IIS.
不幸的是,仅根据 IIS 存储的配置数据做出此决定是不够的。 Every application pool has an associated CLR version, even if no managed code ever runs in the application pool.
每个应用程序池都有一个关联的 CLR 版本,即使没有托管代码在应用程序池中运行。 And the second ASP.NET integration with IIS is enabled on a machine, the default module/handler lists all include ASP.NET entries.
第二个 ASP.NET 与 IIS 集成在一台机器上启用,默认模块/处理程序列表都包括 ASP.NET 条目。
In the case of a library, the heuristic I described would be the way to go.
在图书馆的情况下,我描述的启发式方法将是 go 的方式。
In present times though, what's “an ASP.NET application” anymore?
但是,现在,什么是“ASP.NET 应用程序”? 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".
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".
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.