7

We'd like to split out the javascript from our Razor views (so we can test). Can we locate the .js files near the views they correspond with rather then in the Scripts folder? For example, we'd like to see this in solution explorer:

MyMvcProject
    - Views
      - Home
        - About.cshtml
        - About.js

However, I don't know to references the .js file from the .cshtml view.

Jacob
  • 77,566
  • 24
  • 149
  • 228
Brian Low
  • 11,605
  • 4
  • 58
  • 63
  • 1
    possible duplicate of [Where to put view-specific javascript files in an ASP.NET MVC application?](http://stackoverflow.com/questions/604883/where-to-put-view-specific-javascript-files-in-an-asp-net-mvc-application) – Brian Low Feb 21 '12 at 22:39

3 Answers3

9

For security reasons, asp.net-mvc blocks all file access to the /Views folder from URL's. This can be worked around, but I would suggest NOT doing this for security reasons.

You should generally leave your scripts in Scripts folder, particularly the system-wide ones such as jquery and the unobtrusive stuff. This is so they can be more easily updated through NuGet as new versions or bugfixes are released.

I'm not sure why you have a problem with testing and leaving them in the default location.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • 3
    Keeping tightly coupled code (like view-specific js) together means it is less likely to be missed when modifying. Agreed, common js files should be left in Scripts. What are the security implications of serving js files from the View folder? – Brian Low Feb 21 '12 at 07:14
  • @Brian - The reason access is denied to the views folder from URL's is because view files can contain sensitive information. Allowing access to the folder can expose information you don''t want attackers knowing. One option would be to add an http handler that would filter all .js files through the handler, allowing you to serve them through code and control access. – Erik Funkenbusch Feb 21 '12 at 12:47
3

Open Views/Web.config and replace this

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />

with this

<add name="ExcludeRazorViews" path="*.cshtml" verb="*" type="System.Web.HttpNotFoundHandler" />
Alexey Solonets
  • 817
  • 6
  • 15
1

If your talking about breaking 'custom' scripts out of your view and placing them within a separate folder for the purpose of testing your javascript then yes you can place them in a folder of your choice.

However, I wouldn't recommend placing them side by side your view...that's going to lead to a messy project structure and will make it that much harder if you ever want to minimize your javascript.

I generally leave "framework" javascript files within the Scripts folder for the reasons that Mystere Man mentioned in his answer however, I have put 'custom' or view related javascript files within /Content/js/.

To reference them you would simply add a reference within your view or master page (layout):

<script type="text/javascript" src="@Url.Content("~/Content/js/somelink.js")"></script>
Jesse
  • 8,223
  • 6
  • 49
  • 81
  • It will be harder to minimize because most minimizing tools look in the Scripts folder? – Brian Low Feb 21 '12 at 07:15
  • @Brian - most minimizing tools expect .js files to be in the same location, not necessarily /Scripts – Erik Funkenbusch Feb 21 '12 at 12:48
  • I ultimately don't agree with that putting script files nearby the view leads to a mess. This is traditional project boilerplate structure. And this is very bad for maintenance and scaling. Just the opposite, files should be structured according to client language. It means that all files which relate to the same feature (domain language area) should be placed as closer as possible... – alehro Apr 16 '18 at 10:37