How to set the root path for asp.net application

2019-08-21 13:04发布

I have an asp.net application containing pages that lie in multiple folder. I have my .js files also in one "JS" folder and I have added their reference in head of master page like:

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

Now when I am on home page, the script loads fine. But when I am on some other page that is present in another folder(Physics for example), the path gets appended and hence I get the error:

Failed to load resource: the server responded with a status of 404 (Not Found)

Similar thing is happening for my image paths and <a> tags also.

Now I want to give paths with respect to root path something like:

~/JS/VerticalMenu.js

But this ~ is not taking me to the root of my application. Do I need to set where ~ should lead to? And if yes then where and how??

标签: asp.net root
3条回答
放荡不羁爱自由
2楼-- · 2019-08-21 13:33

Try this:

<script type="text/javascript" 
    src="<%=Page.ResolveUrl("~/JS/VerticalMenu.js")%>"></script>
查看更多
做自己的国王
3楼-- · 2019-08-21 13:36

The @Charlie Kilian answer is a workable solution however you can also specify a base URL for all the relative URLs in your page by base tag in head of your html page.

<head>
    <base href="http://www.yourdomain.com/anyvirtualdirectory/" />
</head>
查看更多
一夜七次
4楼-- · 2019-08-21 13:39

If it's an ASP.NET application, you can access the root by prefixing the path ~/:

<script src="~/common/scripts/safeguard.common.js" type="text/javascript"></script>  

You can also try prefixing the path with just /:

<script src="/common/scripts/safeguard.common.js" type="text/javascript"></script>
查看更多
登录 后发表回答