1

I'm running an MVC project which references a class library. The class library contains a javascript file which build action is set to "Embedded resource".

What actually happens to the file? I can't seem to get it using the ResourceManager because I have no idea of the namespace it puts the resource in.

My class library file structure:

  • /Project
  • /Project/Web
  • /Project/Web/Js
  • /Project/Web/Js/Test.js << embedded resource

Code:

var t = this.GetType().Assembly;
var r = new System.Resources.ResourceManager("Namespace", t); //What is the namespace here?
var js = r.GetObject("Test.js");
2
  • The root namespace of your assembly. Commented Oct 24, 2012 at 22:13
  • Doesn't work. I'm getting the following exception: "Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "YourNamespace.resources" was correctly embedded or linked into assembly "YourNamespace.Project" at compile time, or that all the satellite assemblies required are loadable and fully signed." Commented Oct 24, 2012 at 22:18

1 Answer 1

1

Sorry, I dont have access to a compiler right now so the following might contain slight errors.

var asm = typeof(MyClass).Assembly;
var stream = asm. GetManifestResourceStream("<rootnamespace>.Project.Web.Js.Test.js");
var reader = new StreamReader(stream);
String source = reader.ReadToEnd();

Notes:

  • use typeof(x) instead of GetType(). The latter will give incorrect result if you inherit from the type.
  • rootnamespace can be found/set in the project settings. (I assumed that "Project" is a folder inside the project in my example)
  • asp.net has a pre-build solution for using resources this way. Try looking into Page.ClientScript for this. (I'm not sure if this also works for MVC, but would bet that it would)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.