简体   繁体   中英

js file and css files are not loaded correctly after the url routing in asp.net webforms

I am trying to implement the urlrouting for asp.net webforms. My problem is all the js file and the css files not loaded on the page because it cannot refer to the correct path.

I've refer the js on the my master page as shown below.

 <script type="text/javascript" src="Scripts/jquery-1.7.1.min.js" charset="utf-8"></script>


 <script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js" charset="utf-8"></script>

Could you please help me for a solution.

you can use the Page.ResolveUrl(String relativeUrl)

Page.ResolveUrl("...") will generate an absolute path out of an relative one:

<script type="text/javascript" src='@Page.ResolveUrl("~/Scripts/jquery-1.7.1.min.js")' charset="utf-8"></script>

or

<script type="text/javascript" src='<%= Page.ResolveUrl("~/Scripts/jquery-1.7.1.min.js")%>' charset="utf-8"></script>

depending on if you're using razor markup or not.

This is the correct format to call your JQuery script with. The '~' refers to the root directory. So assuming your script is in this location, it should work.

<script type="text/javascript" src="~/Scripts/jquery-1.7.1.min.js" charset="utf-8"></script>

However, the line above it attempts to load the same script, but does not include the '~' character.

<script type="text/javascript" src="Scripts/jquery-1.7.1.min.js" charset="utf-8"></script>

...and you aren't attempting loading any CSS files. So delete the first line, and then add the correct tags to load your CSS files.

<link rel="stylesheet" type="text/css" href="~/Content/mystyle.css">

Only use / Because it means root of the site. (if Scripts dir under your root)

<script type="text/javascript" src="/Scripts/jquery-1.7.1.min.js" charset="utf-8"></script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM