简体   繁体   中英

Handling URLs correctly — production vs development (ASP.NET)

My production environment is in a directory off the main url www.example.com/sample . Most of my URLs are resolving fine by using ~ , so ~/default.aspx resolves to www.example.com/sample/default.aspx .

However, none of my javascript of css is loading correctly in production. I have the script src set to, for example, js/sample.js , and I have tried ~/js/sample.js and it does not resolve. I tried runat="server" in the <script> tag, but I got an error sometimes. I have:

<script runat="server" src="~/js/sample.js?<%=   MyCode.VersionInfo.JSTag %>" type="text/javascript"></script>

Here I am tagging a version code to the end of the filename to force reload during debugging, however, with runat=server it complains about this.

I'm sure this is a common issue, and there is a standard idiom for handling it, but I can't seem to find it. Can someone help me? When and how does the ~ get expanded, and how do people handle this type of thing?

Most likely when the web site goes to full production, it will go in without the subdirectory, so I need to handle this systematically.

what you are looking for is:

<script type="text/javascript" src="<%= this.ResolveUrl("~/js/sample.js?" + MyCode.VersionInfo.JSTag) %>"></script>

more information here

只需使用js,css和图像的相对路径,即js / sample.js

Here is a suggestion:

<script type="text/javascript" src="<% =HttpRuntime.AppDomainAppVirtualPath & "/js/sample.js"%>"></script>

Additionally, if you would like to discover path issues like this earlier, you can set your web application to run under a virtual directory, under Cassini (the built in webserver).

Right click on the Web Project, choose Properties, choose Web, and then type in your Virtual Path - eg /sample/.

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