1

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.

1
  • 1
    you need to change your rewriting rules to exclude things like js and css Commented Jul 23, 2015 at 13:47

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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">

Comments

0

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>

1 Comment

This don't work in every case... imagine he will publish the homepage in a subfolder i.E.: http://foo.com/bar/. So if he uses / the browser would look for the scripts under http://foo.com instead of http://foo.com/bar/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.