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.

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

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>

3 Comments

It will be harder to minimize because most minimizing tools look in the Scripts folder?
@Brian - most minimizing tools expect .js files to be in the same location, not necessarily /Scripts
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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.