Include javascript from assembly in MVC
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 =
m开发者_StackOverflowyassembly.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?
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);
If it is meant to be rendered on the server only, try returning new ContentResult(jsContent)
.
精彩评论