ASP.Net Master Page and File path issues

2020-01-25 04:04发布

I'm trying to add a script reference to jQuery in my master page so that it will work for any page. It currently looks like this

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

The problem is that the path is always relative to the executing aspx page so this will only work if the "jquery.js" file is located in the same folder. To make it work I have to change the line to:

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

This is obviously less than ideal because it will only work for pages that are two levels deep from the root folder. If I try the following, IIS throws an error about an unexpected character.

<script runat="server" type="text/javascript" src="~/jquery.js"></script>

Any ideas?

EDIT: I forgot to mention as well that the script MUST be in the head tag

The current top answer throws a "ASP.NET Ajax client-side framework failed to load." error when I add it to my master page. Its thrown from javascript and not the .Net compiler. If I move the ScriptManager to the head section where it should be I get a compile error about the ScriptManager needing to be inside a form tag.

The third answer throws a "Illegal characters in path." exception from the compiler

EDIT 2: When I add that line to my head tag I get this error from IIS.

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

SOLVED: I took the edited response from the answer below and put it inside an asp:ContentPlaceHolder element

10条回答
时光不老,我们不散
2楼-- · 2020-01-25 04:16
<body>
<script language="javascript" src='<%= this.ResolveClientUrl("~/full/path/to/jquery.js") %>' type="text/javascript"></script>
</body>
查看更多
冷血范
3楼-- · 2020-01-25 04:17

You could use a ScriptManager:

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

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

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

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

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

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

<head>
    <script type="text/javascript" 
        src="<%# Page.ResolveClientUrl("~/jquery.js") %>"></script>
</head>
查看更多
时光不老,我们不散
4楼-- · 2020-01-25 04:19

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

<script type="text/javascript" src="<%= Page.ResolveClientUrl("~/jquery.js") %>"></script> 
查看更多
再贱就再见
5楼-- · 2020-01-25 04:20
<script type="text/javascript" src="/full/path/to/jquery.js"></script>
查看更多
啃猪蹄的小仙女
6楼-- · 2020-01-25 04:22

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

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

Then in Code Behind of Master page under Page_Load Event

Page.Header.DataBind();

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

查看更多
在下西门庆
7楼-- · 2020-01-25 04:28

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

查看更多
登录 后发表回答