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

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

问题描述

在我的 mvc 3 应用程序中,假设我有一个类似 ViewsAccountjscustom.js 的文件夹结构,我如何将该文件添加到我在 Accountindex 的视图中.cshtml好吗?

In my mvc 3 application, assuming I have a folder structure like ViewsAccountjscustom.js How do I add that file to my view in the Accountindex.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..

谢谢.

推荐答案

/Views 文件夹中的 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>

您可以更改它,但总体而言,不将资产存储在视图文件夹中可能更安全.

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

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

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