Server.MapPath in a COM Component

北城以北 提交于 2019-12-04 10:28:01

Add to your C# project an interop for ASP.dll (you will find it in the \system32\inetsrv folder.

Add a public method to the class that gets instantiated by ASP:-

 ASPTypeLibrary.ScriptingContext context;
 public void OnStartPage(ASPTypeLibrary.ScriptingContext sc)
 {
     context = sc;
 }

Now when you need a MapPath use:-

 context.Server.MapPath("...");

Note context gives you access to the Request, Response and Session in addition to Server. The OnStartPage is a pre-COM+ hack that ASP used and still works even in the latest versions. ASP performs the COM equivalent of reflection (examining the COM classes type library info) to determine if a public OnStartPage method is available, if so it calls it passing the ScriptingContext object.

There is no .NET HttpContext available, the request would have had to have been handled by .NET in the first place for that to exist. A HttpContext cannot be created on a thread "after the fact" as it were. Hence if your component needs to interact with the Http conversation it will have to do so via the ASP context object since ASP is the host that is actually handling the request.

I think indeed changing the behaviour is the best option here... wel... not exactly the behaviour, but the interface of the COM object... Instead of passing in the server context, just pass in the relevant information needed for the method.

Why not pass in the full path to "/somelocation" to your C# COM component? This will help you get rid of some ugly dependencies.


UPDATE: You may want to try HostingEnvironment.MapPath. You need to add a reference to System.Web before using it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!