在类库内部可以通过 System.Web.HttpContext.Current 来判断当前代码是运行于 ASP.net 工程还是 WinForm/Console。
1
using
System.Web;
2![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
if
(System.Web.HttpContext.Current
!=
null
)
6
Debug.WriteLine(
"
运行于 ASP.net
"
);
7
else
8
Debug.WriteLine(
"
运行于 WinForm/Console
"
);
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/dot.gif)
作者: yuhen