简体   繁体   English

正确处理URL - 生产与开发(ASP.NET)

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

My production environment is in a directory off the main url www.example.com/sample . 我的制作环境位于主网址www.example.com/sample的目录中。 Most of my URLs are resolving fine by using ~ , so ~/default.aspx resolves to www.example.com/sample/default.aspx . 我的大部分网址都使用~解析得很好,所以~/default.aspx解析为www.example.com/sample/default.aspx

However, none of my javascript of css is loading correctly in production. 但是,我的css javascript都没有在生产中正确加载。 I have the script src set to, for example, js/sample.js , and I have tried ~/js/sample.js and it does not resolve. 我有脚本src设置为,例如, js/sample.js ,我已经尝试~/js/sample.js ,它没有解决。 I tried runat="server" in the <script> tag, but I got an error sometimes. 我在<script>标记中尝试了runat="server" ,但有时会出错。 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. 在这里,我将一个版本代码标记到文件名的末尾以强制在调试期间重新加载,但是,对于runat=server它会抱怨这一点。

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). 此外,如果您希望发现此前的路径问题,可以将Web应用程序设置为在Cassini(内置Web服务器)下的虚拟目录下运行。

Right click on the Web Project, choose Properties, choose Web, and then type in your Virtual Path - eg /sample/. 右键单击Web项目,选择“属性”,选择“Web”,然后键入虚拟路径 - 例如/ sample /。

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

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