开发者

web application root operator and script tags

开发者 https://www.devze.com 2023-01-13 01:26 出处:网络
Is there any way to use ASP.NET\'s \'web application root\' operator ~ in a script tag? If not, is there any way to mimic such behavior?

Is there any way to use ASP.NET's 'web application root' operator ~ in a script tag? If not, is there any way to mimic such behavior?

My application uses nested master pages for different sub directories; A content page uses its directory-specific master page, which uses the root master page. I'd like to be able to include my <script> tags in the root master page, so I'm not repeating code all开发者_高级运维 over the place, but since I don't necessarily know the depth of the path for any given content page, I can't reliably provide paths to the scripts folder.

I considered using paths in the form /scripts/jquery.js, but since the Visual Studio development server starts the application in a subdirectory of the server root, this will not translate well to the live server. To illustrate:

<!-- dev server path -->
<script type="text/javascript" src="/my_project/scripts/jquery.js"></script>

<!-- live server path -->
<script type="text/javascript" src="/scripts/jquery.js"></script>

You can, of course see the issue. Since I am not the only developer on the project, I have very little control over what happens in the "go live" process; otherwise, it could just be a matter of removing /my_project in the "go live" process.


There are some possible cases on that.

1) For large project use the local iis5.1 or other local iis, and not the VS web that runs.

2) You can avoid the first spash and use relative paths... eg:

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

so you do not force him to start from beggining.

3) You can place a literal control there and just render the script tag on Page_Load with the correct path every time

4) and you can just render the src on page

<script type="text/javascript" src="<%=ResolveUrl("~/scripts/jquery.js")%>" ></script>

I am using the 1 and 3.

0

精彩评论

暂无评论...
验证码 换一张
取 消