在整个 Webresource.axd 中使用嵌入的 WebResources

发布于 2024-11-02 20:59:13 字数 73 浏览 0 评论 0原文

问题很简单:如何在 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 技术交流群。

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

发布评论

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

评论(2

过气美图社 2024-11-09 20:59:13

编辑:对于不引用 Page 和 ClientScript 的版本,请参阅 在 Razor 视图上处理嵌入资源的正确方法是什么?

经过半天的学习,我学到了这些:

  1. 要嵌入资源,需要将其构建操作设置为嵌入资源(在VS解决方案中)资源管理器右键单击文件 -> 属性)

  2. 下一步必须修改AsssemblyInfo.vb以使该资源可用于WebResource 查询。将 [Assembly: System.Web.UI.WebResource("MyWebResourceProj.Test.css", "text/css")] 添加到位于项目的 MyProject 文件夹中的 AssemblyInfo.vb。

    • 名称由根命名空间/程序集名称+'.'+文件名组成。要 100% 确定该名称,请使用以下代码片段进行查找:
      Dim resNames = Assembly.LoadFile("YourDll.dll").GetManifestResourceNames()
    • 请注意,程序集的根命名空间必须与程序集名称相同(这花了我大约 4 个小时才意识到。至少在 .Net v4 中是这样)
    • 如果 css 中有引用 ( <%=WebResource("NS.image.jpg")%> ),则为该 css 的 WebResource 属性传递 PerformSubstitution:=true 。
  3. 可以使用 Page.ClientScript.GetWebResourceUrl(GetType(MyWebResourceProj.ConssumingPage), "MyWebResourceProj.Test.css") 来引用资源

    • 请注意,可以使用 Me.GetType() 代替 GetType(Typename),但同样,如果该类是继承的,则该方法将不起作用,因此请注意!

资源:

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:

  1. to embed a resource one needs to set it's Build Action to Embedded Resource (in VS Solution Explorer rightclick the file -> Properties)

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

    • The name consists of root namespace/assembly name +'.'+filename. To be 100% sure of the name, use the following code snippet to look it up:
      Dim resNames = Assembly.LoadFile("YourDll.dll").GetManifestResourceNames()
    • Note that the assembly's Root Namespace must be the same as the Assembly Name (this took me about 4 hours to realize. At least with .Net v4 that is the case)
    • If there are references inside the css ( <%=WebResource("NS.image.jpg")%> ) than pass PerformSubstitution:=true for that css's WebResource attribute.
  3. Referencing the resource can be done with Page.ClientScript.GetWebResourceUrl(GetType(MyWebResourceProj.ConssumingPage), "MyWebResourceProj.Test.css")

    • Note that instead of GetType(Typename) one could use Me.GetType(), but again, that won't work if the class is inherited, so beware!

Resources:

那片花海 2024-11-09 20:59:13

通过 WebResource.axd 使用嵌入式资源是一件令人头疼的事情,正如您从自己的答案中看到的那样。您必须保持 assemblyinfo.vb|cs 同步,并且似乎总是几乎不可能获得所有名称空间和名称空间。程序集名称位于所有正确的位置。

当您最终让它工作时,您的奖励是包含脚本行,看起来像核心内存转储。

我建议另一种选择。为自己编写一个非常简单的 Web 处理程序(例如 MyResourceLoader.ashx)。然后向您的类添加一个方法,以您认为有意义的任何方式简单地为它自己的嵌入式资源提供服务。您可以使用反射来获取类,就像 WebResource 那样,或者只是将您需要的任何内容硬编码到加载器中,如果它只是为了特定目的,那么您的类中的公共方法可能如下所示:

public static Stream GetResource(string resourceName) {
   // get the resource from myself, which is easy and doesn't require 
   // anything in assemblyinfo, and return it as a stream. As a bonus, 
   // you can parse it dynamically or even return things that aren't
   // just embedded, but generated completely in code!
}

或者如果您决定制作更通用的东西,您可以得到所有的奇特。并使用返回更多数据一个类,例如

class ResourceInfo
{
    public Stream Data;
    public string MimeType;
    public string FileName;
}

现在您可以以任何您想要的方式提供嵌入式资源,例如

我认为 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:

public static Stream GetResource(string resourceName) {
   // get the resource from myself, which is easy and doesn't require 
   // anything in assemblyinfo, and return it as a stream. As a bonus, 
   // you can parse it dynamically or even return things that aren't
   // just embedded, but generated completely in code!
}

Or if you decide to make something more general purpose, you can get all fancy and return more data using a class, e.g.

class ResourceInfo
{
    public Stream Data;
    public string MimeType;
    public string FileName;
}

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.

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