1

Is it possible to include a .js file in the View in asp.net mvc? I use razor engine, MVC 3.

It looks that header is already defined in _Layout.cshtml....So I pretty much have to include all my js files in the layout file?

Daniel A. White
192k49 gold badges387 silver badges472 bronze badges
asked Oct 26, 2011 at 0:23

2 Answers 2

2

You can define an optional section in your layout file. Then in your view define it.

_Layout.cshtml

<head>
 ..
 @RenderSection("Includes", required: false)
 ..
</head>

In your view

@section Includes {
 <script ...
}
answered Oct 26, 2011 at 0:25
Sign up to request clarification or add additional context in comments.

4 Comments

I used your code as is (just added a valid include js statement), and I am getting Section not defined: "Includes" error. Is there anywhere else I need to include it?
it is. Do I need to define the section in every view?
Btw, it is NOT good practice put all scripts in the HEAD. Quite opposite, you should put your script as late as possible, most of right before closing BODY. See developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_5
1

Another way that can be used to organize and load javascript is using the following line in the Layout file.

 <script src="@Url.Content("~/Scripts/ViewScripts/" + 
 ViewContext.RouteData.Values["controller"].ToString() + "." +
 ViewContext.RouteData.Values["action"].ToString() + ".js")" 
 type="text/javascript"></script>

Then you can create separate js files for each controller action/view. For example, if you want to load a script specifically for your homepage using the default controller you create a file called home.index.js or for the about page home.about.js

This gives a way to organize page specific javascript in small manageable files without having to specificallty load the script for every page. If the Controller/Action has a file it will be loaded, if it doesn't it will be skipped.

answered Oct 26, 2011 at 0:52

2 Comments

No downvote because it is sort of creative, but just want to say that this sounds like a really bad idea the more I read it. Not only are you having to keep up with 20, 30, 40 javascript files, but you are completely throwing JS code reuse out the window. Lastly, attempting to load a resource that may or may not result in a 404 is just bad form IMHO.
@Tommy - The 404 is the only drawback to this and the one thing I do not like. The re-use is not an issue; it is only for script that is specific to the controller/view. Also this tag is the last script included in the _Layout so that it can reference any infrastructure scripts that are used in the site. For the number of files, if you do not like smaller chunks of script you can leave out the action value and only have a script per controller which will require less files.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.