MVC - 在视图文件夹中访问css,image,js文件

时间:2022-07-29 11:20:50

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?

在我的mvc 3应用程序中,假设我有一个像\ Views \ Account \ js \ custom.js这样的文件夹结构。如何在Account \ index.cshtml中将该文件添加到我的视图中?

I have tried:

我努力了:

<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>

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)

但似乎没什么用,萤火虫总是说404文件没有在我指定的地方找不到。 (有时它会在路径中添加额外的视图:-s)

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..

我知道我可以将它放在我自己的文件夹中的视图之外并像/myFolder/myfile.js一样访问它并且它可以工作但是这个javascript文件与帐户视图中发生的事情非常密切相关,所以它会使把感觉放在那里..

thanks.

谢谢。

4 个解决方案

#1


16  

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

/ Views文件夹中的web.config文件默认限制对文件夹中文件的所有访问:

<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.

您可以更改它,但总体上可能更安全,不将资源存储在views文件夹中。

#2


2  

You can use a UrlHelper:

您可以使用UrlHelper:

<script src="@Url.Content("~/view/account/js/custom.js")" type="text/javascript"></script>

#3


0  

The best way is to use T4MVC - http://mvccontrib.codeplex.com/wikipage?title=T4MVC No need to use magic strings...

最好的方法是使用T4MVC - http://mvccontrib.codeplex.com/wikipage?title=T4MVC无需使用魔术字符串......

#4


0  

Add this under your views web.config Handlers section

在您的视图下添加此web.config处理程序部分

<add name="JavaScriptHandler" path="*.js" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />

#1


16  

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

/ Views文件夹中的web.config文件默认限制对文件夹中文件的所有访问:

<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.

您可以更改它,但总体上可能更安全,不将资源存储在views文件夹中。

#2


2  

You can use a UrlHelper:

您可以使用UrlHelper:

<script src="@Url.Content("~/view/account/js/custom.js")" type="text/javascript"></script>

#3


0  

The best way is to use T4MVC - http://mvccontrib.codeplex.com/wikipage?title=T4MVC No need to use magic strings...

最好的方法是使用T4MVC - http://mvccontrib.codeplex.com/wikipage?title=T4MVC无需使用魔术字符串......

#4


0  

Add this under your views web.config Handlers section

在您的视图下添加此web.config处理程序部分

<add name="JavaScriptHandler" path="*.js" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />