MVC - 访问在视图文件夹的css,图像,js文件 [英] MVC - Accessing css, image, js files in view folder

查看:123
本文介绍了MVC - 访问在视图文件夹的css,图像,js文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC 3应用程序,假设我有一个文件夹结构,如 \\浏览\\帐户\\ JS \\ custom.js 如何将该文件添加到我的观点在帐户\\ index.cshtml 吗?

In my mvc 3 application, assuming I have a folder structure like \Views\Account\js\custom.js How do I add that file to my view in the Account\index.cshtml please?

我曾尝试:

<script src="js/custom.js" type="text/javascript"></script>
<script src="/views/account/js/custom.js" type="text/javascript"></script>
<script src="~views/account/js/custom.js" type="text/javascript"></script>

但似乎没有任何工作,萤火总是说在这一点不像我指定的那些地方找不到404文件。 (有时它添加路径额外观点:-s)

but nothing seems to work, firebug always says 404 file not found in places that are nothing like the ones I specify. (sometimes it adds extra view in the path :-s)

我知道我可以把它放在视图外,我自己的文件夹和访问它像 /myFolder/myfile.js ,它会工作,但这个JavaScript文件是非常密切与在帐户视图,没有别的什么事情,所以这将是有意义的把它放在那里。

I know I can put it outside the view in my own folder and access it like /myFolder/myfile.js and it would work but this javascript file is very intimately related to what's going on in account view and nothing else, so it would make sense to put it there..

感谢。

推荐答案

在/浏览文件夹中的的web.config 文件限制默认情况下所有的文件访问的文件夹中

The web.config file in the /Views folder restricts all access to files in the folder by default:

<httpHandlers>
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

您可以改变这种状况,但它可能是更安全的全面的资产不存储在views文件夹。

You could change that, but it's probably more secure overall to not store the assets in the views folder.

这篇关于MVC - 访问在视图文件夹的css,图像,js文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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