简体   繁体   中英

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. 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:

在此处输入图片说明

Now, you need to add a reference to the System.Web assembly and using System.Web to the file:

在此处输入图片说明

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. 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. 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. If the caller is a desktop application it should use something else. If it is a Windows Phone application it should use another thing. See how now your DAL, BAL, ... is agnostic of the caller and far more reusable?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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