简体   繁体   English

为什么system.web不可用?

[英]why is system.web not available?

looking to use : 希望使用:

fileMap.ExeConfigFilename = System.Web.HttpContext.Current.Server.MapPath("~/Synchroniser/ConvertXML/App.config");

in my project. 在我的项目中。 .HttpContext is flagging as namespace does not exist. .HttpContext正在标记为名称空间不存在。 I have looked at an old project where this works and have imported the same ref file to my references, but it is flagging with a yellow explainataion mark, also tried adding 我查看了一个旧项目,该项目可以正常工作,并且已将相同的参考文件导入到我的参考文件中,但是它带有黄色的解释标记,也尝试添加

    using System.Web.Http;
using System.Web;

but no joy, any idea? 但没有喜悦,有什么主意吗?

Make sure that your project is targeting the full .NET 4.0 instead of the Client Profile: 确保您的项目针对的是完整的.NET 4.0而不是客户端配置文件:

在此处输入图片说明

Now, you need to add a reference to the System.Web assembly and using System.Web to the file: 现在,您需要添加对System.Web程序集的引用,并using System.Web到文件:

在此处输入图片说明

All this being said, please note that it's an absolutely abominable thing to do. 话虽如此,请注意,这绝对是一件令人讨厌的事情。 Your class libraries (aka DAL, BAL, whatever ...) should have strictly no knowledge about any HttpContext. 您的类库(又名DAL,BAL等)应该完全不了解任何HttpContext。 That's purely web stuff and should live only where it belongs - to the web tier. 那纯粹是网络内容,仅应存在于其所属的位置-属于网络层。

Your DAL, BAL, ... should not be doing any System.Web.HttpContext.Current.Server.MapPath calls. 您的DAL,BAL,...不应进行任何System.Web.HttpContext.Current.Server.MapPath调用。 It should simply be taking the filename as parameter. 它应该只是将文件名作为参数。 The way this filename is calculated is purely the responsibility of the caller. 该文件名的计算方式完全是调用方的责任。 So if the caller is a web application it could use the MapPath method. 因此,如果调用方是Web应用程序,则可以使用MapPath方法。 If the caller is a desktop application it should use something else. 如果调用方是桌面应用程序,则应使用其他内容。 If it is a Windows Phone application it should use another thing. 如果它是Windows Phone应用程序,则应使用其他工具。 See how now your DAL, BAL, ... is agnostic of the caller and far more reusable? 看看现在您的DAL,BAL ...如何与调用者无关,并且可以重用得多?

HttpContext.Current.Server.MapPath应该替换为HostingEnvironment.MapPath和ref System.Web.Hosting以避免使用httpcontext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM