简体   繁体   English

在asp.net webforms中的url路由后,未正确加载js文件和css文件

[英]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. 我正在尝试为asp.net webforms实现urlrouting。 My problem is all the js file and the css files not loaded on the page because it cannot refer to the correct path. 我的问题是页面上没有加载所有js文件和css文件,因为它无法引用正确的路径。

I've refer the js on the my master page as shown below. 我在我的母版页上引用了js,如下所示。

 <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(String relativeUrl)

Page.ResolveUrl("...") will generate an absolute path out of an relative one: Page.ResolveUrl("...")将生成一个相对的绝对路径:

<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. 取决于你是否使用razor markup

This is the correct format to call your JQuery script with. 这是调用JQuery脚本的正确格式。 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. ...而且您没有尝试加载任何CSS文件。 So delete the first line, and then add the correct tags to load your CSS files. 因此,删除第一行,然后添加正确的标记以加载CSS文件。

<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) (如果Scripts dir在你的根目录下)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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