ASPX:获取cs文件的路径

发布于 2024-09-17 18:49:42 字数 292 浏览 11 评论 0原文

如何获取aspx中cs文件的路径?

问题是:我在 property.cs 文件中创建了一个属性类,该文件在 EPiServer 管理模块中使用。 Server.MapPath() 因此将返回执行文件的路径,该路径与我的代码完全不同。我需要获取 property.cs 文件的路径(从 property.cs 文件内部),以便动态设置 css 和 js 文件的一些相对路径。这怎么能做到呢?

因此,我希望能够在此 cs 文件中包含 .js 和 .css 文件,所有文件都位于同一目录中,但 cs 文件是从 EPiServer UI 访问的。

How can I get the path of a cs file in aspx?

Here's the issue: I've created a property class in a property.cs file which is used in an EPiServer admin module. Server.MapPath() will hence return the path of the executing file, which is in a totally different place than my code. I need to get the path of the property.cs file (from inside the property.cs file) in order to dynamically set some relative paths to css and js files. How can this be done?

I hence want to be able to include .js and .css files in this cs file, all files located in the same directory, but the cs file is accessed from the EPiServer UI.

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

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

发布评论

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

评论(3

不奢求什么 2024-09-24 18:49:42

我强烈建议不要做你想做的事。您将构建对文件的脆弱依赖,而这些文件甚至不应该与您的项目一起部署。

如果您的 Web 类依赖于 javascript 和 css 等资源,则应使用 ClientScriptManager(或用于 ajax 应用程序的 ScriptManager)来 将脚本文件注册到页面上,并且脚本和CSS应该部署到它们自己的常规Web目录中。

如果部署位置是一个问题,并且您正在创建某种可重用、可再发行的模块,那么我建议您将 .js 和 .css 文件嵌入为 WebResources 在程序集中,并使用脚本管理器将脚本注册到这样的页面,使用 ClientScriptManager.RegisterScriptResource( )。

I would highly recommend not doing what you are trying to do. You'll be constructing a brittle dependency on files that should not even by deployed with your project.

If you have web classes that rely on resources like javascript and css, you should use the ClientScriptManager (or ScriptManager for ajax apps) to register the script files onto the page, and the scripts and css should be deployed into their own regular web directory.

If deployment location is a problem, and you're creating some kind of reusable, redistributable module, then I would recommend that you embed the .js and .css files as WebResources in your assembly, and use the script manager to register the scripts to the page that way, with ClientScriptManager.RegisterScriptResource().

寂寞笑我太脆弱 2024-09-24 18:49:42

我认为您应该将 CSS、JS 和其他静态文件作为嵌入资源包含进来。

这将包括 DLL 内的文件,从而使部署更加容易。然后,您可以设置一个 HTTP 处理程序,它将为您提供嵌入文件的内容 - 或使用前面提到的 RegisterScriptResource() 方法。

通过嵌入文件,您不必知道任何文件路径

I think you should go with including your CSS, JS and other static files as embedded resources.

That will include the files inside the DLL, which makes deployment easier. You can then set up a HTTP handler which will serve you the contents of the embedded files - or use the aforementioned RegisterScriptResource() method.

By embedded the files you don't have to know any file paths.

小情绪 2024-09-24 18:49:42

ASP.NET 已编译,您永远不需要在运行时从文件中读取设置,您应该从配置文件 (*.config) 中读取设置(如果需要)动态的,这些也可以在页面生命周期中通过各种方法注入。

ASP.NET is compiled, you should never need to read settings from source files at runtime, you should read from configuration files (*.config), if they need to be dynamic, these too can be injected during the page lifecycle via a variety of methods.

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