最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Best practices, How to specify relative path for css,javascript and images files? - Stack Overflow

programmeradmin4浏览0评论

I have a question about relative path to external javascript, css and images files in Asp.C# application.

I have pages with 2 - 5 sub levels so my javascript and css files looks like

../../../../../javascriptfile.js
../../../cssfile.css
../../../../../../image.jpg

In case if it will be additional sub level application won't find files.

What is the best practices to specify path to file???

I have a question about relative path to external javascript, css and images files in Asp.C# application.

I have pages with 2 - 5 sub levels so my javascript and css files looks like

../../../../../javascriptfile.js
../../../cssfile.css
../../../../../../image.jpg

In case if it will be additional sub level application won't find files.

What is the best practices to specify path to file???

Share Improve this question asked Mar 6, 2011 at 17:08 Anthony FernandesAnthony Fernandes 111 silver badge2 bronze badges 1
  • the answer will be just to reorganize the files to reduce the usage of such relative path :) – Ant's Commented Mar 6, 2011 at 17:13
Add a ment  | 

3 Answers 3

Reset to default 6

Keep in mind when considering these answers that "root-relative" and "root of the site" may really mean the root of the path following the domain name in the url. You may need to take into account scenarios where your web site is not located at the root. In such scenarios, root-relative paths would potentially point to a different web site.

In ASP.NET you can use a leading ~ to generate urls relative to the root of current site for most server-side controls, as in: <img src="~/image.jpg" runat="server">

You can also use the ResolveUrl method (and other similar methods) to expand such paths without using server-side controls.

Use a root-relative path like this:

    /js/javascriptfile.js
    /css/style.css

the first / means at the root of the site.

Use absolute or root-relative paths to avoid confusion in multi-level pages.

Look here for more information: http://www.motive.co.nz/glossary/linking.php

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论