开发者

Relocating files related to .Net MVC3 Unobtrusive AJAX

开发者 https://www.devze.com 2023-02-04 22:57 出处:网络
A simple question (I hope): We have a set structure within our projects for Assets, that being /Assets/img/, /Assets/css/ and /Assets/js/

A simple question (I hope):

We have a set structure within our projects for Assets, that being /Assets/img/, /Assets/css/ and /Assets/js/

I've been reading Brad Wilson's excellent article on Unobtrusive AJAX, in which he mentions the files required, FTA:

In addition to setting the flag, you will also need to include two script files: jQuery (~/Scripts/jquery-1.4.1.js) and the MVC plug开发者_StackOverflowin for unobtrusive Ajax with jQuery (~/Scripts/jquery.unobtrusive-ajax.js).

Is anyone aware of a way of informing MVC to look in a folder other than /Scripts/ for these files - I don't want to add a whole folder in the root of the project just for these 2 files.

UPDATE Oh dear, end of the day brain-rot obviously. Sorry all!

Many thanks


In your view, you can use whatever path you want to set up for your scripts:

<script src="@Url.Content("~/Scripts/jquery-1.4.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

I have mine pointed to the Scripts folder as that is what was set up by default, but you can change that to your specific path.


the way that I have seen it done is to create an extension method on the HtmlHelper class for including javascript.

public static string Script(this HtmlHelper helper, string filename)
{
  if(!filename.EndsWith(".js")) filename += ".js";
  var path = string.Format("<script src='/Assets/js/{0}' type="text/javascript"></script>", filename);
  return path;
}

then in your master page you can add this block to the header

<%= Html.Script("jquery-1.4.1") %>
<%= Html.Script("jquery.unobtrusive-ajax.min") %>
0

精彩评论

暂无评论...
验证码 换一张
取 消