19

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?

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)

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

thanks.

pjumble
  • 16,880
  • 6
  • 43
  • 51
LocustHorde
  • 6,361
  • 16
  • 65
  • 94

4 Answers4

16

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.

pjumble
  • 16,880
  • 6
  • 43
  • 51
  • hm... yea, modifying that particular behaviour of web.config seems like a foolish thing to do.. I will just put this js file with the rest of my js files.. thanks very much. – LocustHorde Feb 27 '12 at 17:02
  • 9
    See http://stackoverflow.com/questions/604883/where-to-put-view-specific-javascript-files-in-an-asp-net-mvc-application for an example of how to explicitly allow just js files to be served from the Views folder. – slolife Apr 25 '13 at 17:47
2

You can use a UrlHelper:

<script src="@Url.Content("~/view/account/js/custom.js")" type="text/javascript"></script>
Lester
  • 4,243
  • 2
  • 27
  • 31
  • Hi, I gave this a go, but it's still coming up as 404.. I tried to look at the resolved path on view-source and it looks correct.. so may be [pjumble's answer](http://stackoverflow.com/a/9468864/435867) is correct :( – LocustHorde Feb 27 '12 at 17:01
0

Add this under your views web.config Handlers section

<add name="JavaScriptHandler" path="*.js" verb="*" preCondition="integratedMode" type="System.Web.StaticFileHandler" />
siphab
  • 451
  • 4
  • 11
0

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

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • yes, I already have that, but the trouble is that my js file is not in ``scripts`` or ``content`` folder, rather inside a view folder itself, and there isn't anything for me to fetch that file. – LocustHorde Feb 27 '12 at 16:50
  • You can change the T4 settings file that comes with T4MVC to include folders other than Scripts and Content. – Jakub Konecki Feb 27 '12 at 17:27
  • I see.. but even so, as mentioned in [pjumbl's answer](http://stackoverflow.com/a/9468864/435867).. it would still be inaccessible! Thank you though. – LocustHorde Feb 27 '12 at 18:08