0

Is there any way that I could include a javascript file that is an embedded resource of an assembly in my Views for MVC 3?

I have tried to create an Action that gets the embedded resource from the assembly and then returns a FileStreamResult like so...

 public FileStreamResult Scripts()
 {
    System.Reflection.Assembly myassembly;
    myassembly = System.Reflection.Assembly.GetExecutingAssembly();
    System.IO.Stream file =
       myassembly.GetManifestResourceStream("Testing.testscript.js");
    return File(file, "application/x-javascript");
 }

Then in my master view:

  <script type="text/javascript">

    @Html.RenderAction("Scripts")

  </script>

This doesn't seem too work.

I would rather just be able to get the embedded resource as a link and then use that as my src value for the script. So something like:

<script src="@Html.GetEmbeddedResourceAsLink("Testing.testscript.js")"/>

Anyone know how?

2
  • This doesn't seem too work. Why? What error are u getting? Commented Apr 21, 2011 at 16:06
  • Its embeddeding the script inline and the page does not render in IE because of a MIME error. I tried a "text/javascript", "application/x-javascript" and "javascript"... This does work in FF4, but I would rather the script not be inline anyway, but rather a link. Commented Apr 21, 2011 at 16:09

2 Answers 2

2

The GetWebResourceUrl might be useful here: http://msdn.microsoft.com/en-us/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx

The example code on that MSDN page is for WebForms but I think the following might work:

string rsname = "MyAssembly.my_script.js";

string url = ((Page)HttpContext.CurrentHandler)
    .ClientScript.GetWebResourceUrl(this.GetType(), rsname);
Sign up to request clarification or add additional context in comments.

Comments

0

If it is meant to be rendered on the server only, try returning new ContentResult(jsContent).

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.