反射以避免 System.Web 引用

发布于 2024-12-06 07:48:05 字数 939 浏览 4 评论 0原文

我有一个在 Windows 和 Web 中使用的共享报告 DLL。我现在尝试将 Windows 程序移动到 .NET 4 客户端配置文件,因此需要避免 System.Web 引用。

在某些时候,代码需要获取网站的目录或 dll 的目录。我使用了类似这样的代码:

string path;
if (HttpContext.Current != null)
{
    path = HttpContext.Current.Server.MapPath("default.aspx");
}
else
{
    path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}

有没有办法使用反射或类似的方法来避免 System.Web 引用,以便我仍然可以获得适当的路径?如果是这样我该怎么做?

还有其他选择吗?

编辑 我想这样做的原因是我使用一个报告系统,该系统可以获取样式表文件并将其应用到报告(我对所有报告都这样做)。作为我之前在 dll 本身中的设置 mystylesheet.repss。因此,如果我想更改样式,我只需在 dll 中更改它,并将其应用于所有报告。只需将适当的 repss 文件放入 Windows 和网站的根目录即可。并找到通往它们的适当路径。

尝试对 dll 使用相对路径会导致问题。通过报告。\mystylesheet.repss 在 Windows 中工作正常,但尝试 ~/mystylesheet.repss .\mystylesheet.repss 或我能从网络中的 dll 中想到的任何其他内容最终会在错误的目录“c:\windows\system32”中查找\inetsrv"。

我可以将设置移动到每个不同的窗口和网络应用程序,并通过计算出的完整路径将其传递进去,当它实际上是 dll 的内部设置时,这样做似乎是倒退的。

希望这一切都有意义。

I have a shared reporting dll that is used in both windows and web. I am now trying to move the windows programs to .NET 4 client profile so need to avoid System.Web references.

At some points the code needs to get the directory of the website or the directory of the dll. I used code something like this:

string path;
if (HttpContext.Current != null)
{
    path = HttpContext.Current.Server.MapPath("default.aspx");
}
else
{
    path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
}

Is there a way to use reflection or similar to avoid the System.Web reference so I can still get the appropriate path? If so how do I do it?

Any alternatives?

EDIT
The reason I want to do this is I use a reporting system that can take a stylesheet file and apply it to reports (I do this for all reports). As a setting I previously had in the dll itself mystylesheet.repss.So if I wanted to change styles I just change it in the dll and it is applied to all reports. It was then just a case of dropping the appropriate repss file into the root directory on windows and the website. And finding the appropriate path to them.

Trying to use relative paths with the dll results in issues. Passing the report.\mystylesheet.repss works fine in Windows but trying ~/mystylesheet.repss .\mystylesheet.repss or anything else I can think of from the dll in web ends up looking in the wrong directory "c:\windows\system32\inetsrv".

I could move the setting out to each different windows and web app and pass it in with the full path worked out it seems backwards to do it that way when it is really an internal setting for the dll.

Hope that has all made sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对岸观火 2024-12-13 07:48:05

为什么不使用相对于 AppDomain.BaseDirectory

这将是 ASP.NET Web 应用程序的根目录,以及包含控制台或 WinForms 应用程序的可执行文件的目录。

对于其他应用程序类型,它通常是一个合理的默认位置:例如,在 VSTO 2005 应用程序中,它将是包含应用程序的 VSTO 托管程序集的目录,而不是 Excel 可执行文件的路径。

如果合适,您可以支持可选的配置设置(例如 appSetting),以允许 DLL 的调用者指定备用位置,同时默认为基目录。

另一种选择是允许调用者指定样式表文件的路径,该路径可以是绝对路径或相对路径。如果是相对的,则使其相对于AppDomain.BaseDirectory

if (!Path.IsPathRooted(stylesheetPath))
{
     stylesheetPath = Path.Combine(
                          AppDomain.CurrentDomain.BaseDirectory, 
                          stylesheetPath);
}
...

请注意,如果使用相对路径,它将相对于当前工作目录,该目录可以在应用程序的生命周期内更改,并且与应用程序基目录不同。

Why don't you use paths relative to AppDomain.BaseDirectory.

This will be the root directory of the web application for ASP.NET, and the directory containing your executable for a Console or WinForms application.

For other application types, it will generally be a sensible default location: for example, in a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your app, not, say, the path to the Excel executable.

If appropriate, you could support an optional configuration setting (e.g. appSetting) to allow the caller of your DLL to specify an alternate location, while defaulting to the base directory.

Another option is to allow your callers to specify a path to the stylesheet file, which can be absolute or relative. If relative, make it relative to AppDomain.BaseDirectory.

if (!Path.IsPathRooted(stylesheetPath))
{
     stylesheetPath = Path.Combine(
                          AppDomain.CurrentDomain.BaseDirectory, 
                          stylesheetPath);
}
...

Note that if you use a relative path, it will be relative to the current working directory, which can change during the lifetime of an application, and is not the same as the application base directory.

悟红尘 2024-12-13 07:48:05

如果确定位置的机制应取决于上下文,那么听起来调用者将其作为构造函数或方法参数或类似的东西传递是合适的。无论这只是一条直线路径还是像 Func 作为“路径解析器”,将完全取决于您需要做什么。

If the mechanism for determining the location should depend on the context, it sounds like it would be appropriate for the caller to pass it in as a constructor or method parameter or something similar. Whether that's just as a straight path or something like a Func<string, string> as a "path resolver" will depend on exactly what you need to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文