在整个 Webresource.axd 中使用嵌入的 WebResources
问题很简单:如何在 ASP.NET 应用程序中使用嵌入式资源?将资源包含在程序集中的步骤是什么,以及如何引用它?可能会遇到哪些问题?
The question's simple: how could one use embedded resources in asp.net applications? What are the steps to include a resource in the assembly, and how to reference it? What are the gotchas that could be encountered?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:对于不引用 Page 和 ClientScript 的版本,请参阅 在 Razor 视图上处理嵌入资源的正确方法是什么?
经过半天的学习,我学到了这些:
要嵌入资源,需要将其构建操作设置为嵌入资源(在VS解决方案中)资源管理器右键单击文件 -> 属性)
下一步必须修改AsssemblyInfo.vb以使该资源可用于WebResource 查询。将
[Assembly: System.Web.UI.WebResource("MyWebResourceProj.Test.css", "text/css")]
添加到位于项目的 MyProject 文件夹中的 AssemblyInfo.vb。Dim resNames = Assembly.LoadFile("YourDll.dll").GetManifestResourceNames()
可以使用 Page.ClientScript.GetWebResourceUrl(GetType(MyWebResourceProj.ConssumingPage), "MyWebResourceProj.Test.css") 来引用资源
资源:
Edit: For a version without referencing Page and ClientScript, see What is the right way to handle Embedded Resources on a Razor View?
After spending a half of a day I've learned these:
to embed a resource one needs to set it's Build Action to Embedded Resource (in VS Solution Explorer rightclick the file -> Properties)
next AsssemblyInfo.vb must be modified to make this resources available for WebResource queries. Add
[Assembly: System.Web.UI.WebResource("MyWebResourceProj.Test.css", "text/css")]
to AssemblyInfo.vb located in MyProject folder of the project.Dim resNames = Assembly.LoadFile("YourDll.dll").GetManifestResourceNames()
Referencing the resource can be done with
Page.ClientScript.GetWebResourceUrl(GetType(MyWebResourceProj.ConssumingPage), "MyWebResourceProj.Test.css")
Resources:
通过 WebResource.axd 使用嵌入式资源是一件令人头疼的事情,正如您从自己的答案中看到的那样。您必须保持 assemblyinfo.vb|cs 同步,并且似乎总是几乎不可能获得所有名称空间和名称空间。程序集名称位于所有正确的位置。
当您最终让它工作时,您的奖励是包含脚本行,看起来像核心内存转储。
我建议另一种选择。为自己编写一个非常简单的 Web 处理程序(例如
MyResourceLoader.ashx
)。然后向您的类添加一个方法,以您认为有意义的任何方式简单地为它自己的嵌入式资源提供服务。您可以使用反射来获取类,就像 WebResource 那样,或者只是将您需要的任何内容硬编码到加载器中,如果它只是为了特定目的,那么您的类中的公共方法可能如下所示:或者如果您决定制作更通用的东西,您可以得到所有的奇特。并使用返回更多数据一个类,例如
现在您可以以任何您想要的方式提供嵌入式资源,例如
我认为 MS 把 WebResource 业务搞得一团糟,幸运的是,做你自己的事情非常简单。
Using embedded resources through WebResource.axd is a pain in the neck, as you can see from your own answer. You have to keep
assemblyinfo.vb|cs
in sync, and it always seems damn near impossible to get all the namespace & assembly names right in all the right places.When you finally get it to work, your reward is an include script line that that looks like a core memory dump.
I suggest an alternative. Write yourself a very simple web handler (e.g.
MyResourceLoader.ashx
. Then add a method to your class that simply serves it's own embedded resources, in whatever way you think is meaningful. You can use reflection to get the classes, like WebResource does, or just hardcode whatever you need into your loader, if it's just for a specific purpose. A public method in your class might look like:Or if you decide to make something more general purpose, you can get all fancy and return more data using a class, e.g.
Now you have the ability to serve up your embedded resources any way you want, e.g.
<script language="javascript" src="/MyResourceLoader.ashx/MyControlScript.js">
I think MS made a mess of that WebResource business. Luckily its' pretty straightforward to do your own thing.