[ACCEPTED]-ASP.Net Master Page and File path issues-runatserver

Accepted answer
Score: 111

You could use a ScriptManager:

<asp:ScriptManager ID="ScriptManager1" runat="server">
        <asp:ScriptReference Path="~/jquery.js" />

EDIT: If you absolutely need this in your 6 <head> section, you could do something like:

    <script type="text/javascript" 
        src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script>

EDIT 2: According 5 to the comments, if you are observing that

The 4 Controls collection cannot be modified because 3 the control contains code blocks (i.e. <% ... %>)

you 2 may need to change the above to use the 1 data-binding syntax:

    <script type="text/javascript" 
        src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
Score: 27

Try <%# instead of <%= in Master page under head 5 section

<script type="text/javascript" 
        src="<%# ResolveUrl("~/YourScriptFolder/YourJQueryOrJavascript.js") %>">

Then in Code Behind of Master page 4 under Page_Load Event


Now you are good to go with 3 either jQuery and JavaScript as well as 2 CSS just you need to change your path in 1 ResolveUrl which file you want to handle CSS, JavaScript, jQuery.

Score: 12

If you're not going to us asp:ScriptManager 2 or absolute paths then you can do it like 1 this:

<script runat="server" type="text/javascript" 
  src='<%= Page.ResolveUrl("~/jquery.js") %>'></script>
Score: 3

I do not know whether you guys found the 21 solution to your problem or not. I was 20 facing the same problem and going nuts to 19 figure out why do I get "jQuery is undefined" error 18 on the plugins i use. I tried all the solutions 17 i get from the internet but no luck at all.

But, suddenly 16 something splash on my mind that may be 15 the script files should be in order. So, I 14 moved the jquery referece to first position 13 and everything start working like charm.

Remember 12 guys, if you're using any plugins with jquery, make 11 sure you use the folloing order of setting 10 reference to those fiels.

  1. reference to the jquery library
  2. reference to the other subsequent plug-in libraries and so on...


  1. "script src="js/jquery-1.3.2.min.js" type="text/javascript"...
  2. "script src="js/jqDnR.min.js" type="text/javascript"...
  3. "script src="js/jquery.jqpopup.min.js" type="text/javascript"...
  4. "script src="js/jquery.bgiframe.min.js" type="text/javascript"...

Always make 9 sure you must put the jquery reference to 8 first and then the subsequent libraries.

Hope, this 7 solves your problem especially when you 6 use with MasterPages. Its very strange 5 that it works no matter what order you use 4 when you don't use MasterPages but when 3 you do, then it somehow requres the proper 2 order.

Good luck and happy coding,

Vincent 1 D'Souza

Score: 3

Look at How to Run a Root “/”. This should fix all your issues 5 regarding unresolved .js file paths. You basically 4 reconfigure the VS Dev server to run your 3 application as localhost:port/ as opposed to the regular 2 localhost:port/application name/ making name resolution work the same way 1 as it does on IIS.

Score: 1

For absolute path of the file for any page 1 use it following:

<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script> 
Score: 0
<script type="text/javascript" src="/full/path/to/jquery.js"></script>


Score: 0

If this script tag goes directly to the 3 browser, then you unlikely can substitute 2 your site's root there. At least not on 1 the server. So you can:

  1. Deploy site to the root of domain name and use absolute paths (simplest solution).
  2. Insert this link with server control.
  3. Preprocess resulting HTML before sending it to the client (with HttpResponse.Filter).
Score: 0

You can also use <base> HTML tag:

<base href="http://www.domain.com"></base>  

and 5 then all the links in header section are 4 relative to base address:

<script type="text/javascript" src="scripts/jquery.js"></script>

It's often useful 3 when you have multiple publishing destinations, like 2 local dev web server, demo server, etc. You 1 just replace that base URL.

Score: 0
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>


More Related questions