Let's say I have a file located at C:\foo\bar.js
and I want to include this file in an ASP.NET MVC website that is, obviously, hosted on the same machine.
This doesn't work:
<script src="C:\foo\bar.js"></script>
And neither does this:
<script src="file:///C:/foo/bar.js"></script>
The above two lines do not make sense either since they would look at the client's folder, not the server's folder.
So how can I serve this file from an ASP.NET MVC Controller (in the controller, I have the string value of the path of the local ph开发者_高级运维ysical file i.e. C:\foo\bar.js
) to a View? Maybe something with an HttpHandler?
Note: This solution is assuming that the js file is not part of the web app. If it is, you should probably be using Url.Content or ResolveClientUrl instead..
You should be able to link to a controller action Url in your script tag:
<script src="/ControllerName/GetJavascriptFile"></script>
...and have an action like::
public ActionResult GetJavascriptFile() {
string mp = @"C:\foo\bar.js";
return File(mp, "text/javascript");
}
http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file(VS.90).aspx
The way I've been doing it is - suppose you have a folder called Scripts
off of the root (so, parallel to Controllers
, Models
, etc.) and you store the JavaScript files in it, you can use this:
<script
src="<%=Url.Content("~/Scripts/jQuery-1.3.2.js")%>"
type="text/javascript">
</script>
(split onto multiple lines to make it easier to read)
This is assuming your question is how to link to the location of the .js
include files, but a similar trick can be applied to any content files (i.e., plugin installers or whatnot)
精彩评论