如何纳入asp.net MVC js文件,并且对所有路线的有效路径 [英] How to include js files in asp.net MVC and have a valid path on all routes

查看:104
本文介绍了如何纳入asp.net MVC js文件,并且对所有路线的有效路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个默认的ASP.net MVC项目。在母版页我有在顶部以下

I created a default ASP.net MVC project. In the Master page I have the following at the top

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>

然后我需要添加一个JavaScript文件,并添加了一行从Solution Explorer拖动文件到页面如下:

I then need to add a javascript file and added the line as follows by dragging the file from the solution explorer to the page:

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
</head>

当我访问该网站,看看从我看到这个浏览器的HTML:

When I access a the site and look at the html from the browser I see this:

<head><title> 

    Index

</title><link href="Content/Site.css" rel="stylesheet" type="text/css" /> 
    <script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>  
</head>

CSS文件相对路径是固定的,但JS文件没有。该网站将被部署到不同的文件夹服务器不是根,我得到我的发展框。

The CSS file relative path was fixed but the JS file not. The site will be deployed to a different folder on the server than the root which I get on my development box.

有没有这样做一个适当的方式?

Is there a proper way to do this?

推荐答案

使用的网址助手:

<script type="text/javascript" src="<%=Url.Content("~/Content/script/MyFile.js")%>"></script>

希望帮助

这篇关于如何纳入asp.net MVC js文件,并且对所有路线的有效路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆