2

When inside a View, within MVC, and you have your JavaScript, I tried to move it to a JavaScript file in the same directory, and it reports it can not file the javascript file

As an example I user NerdDinner and the DinnerForm.aspx, created a JavaScript file in the same directory as the DinnerForm.aspx called DinnerForm.js and referenced it in the aspx file, as below:

Here is the issue, I need to keep my JavaScript file near if not in the same directory in the project as DinnerForm.aspx, how do I go about doing this?

I don't want to place the full path, eg: "~\views\dinners\dinnerform.js" as this is just a work around, and will cause issues later

Any ideas?

asked Sep 25, 2009 at 8:19

2 Answers 2

4

You can't have the JavaScript files in the Views folder as it does not actually exist to the browser. If you have a look at the Web.config file in the Views folder, you'll see that it is set to the HttpNotFoundHandler handler. That means it returns 404 for all requests to its folder.

Why not put them in the Scripts folder? You could always have sub folder under it.

E.g. /Scripts/Dinner/dinnerform.js

answered Sep 25, 2009 at 8:51
Sign up to request clarification or add additional context in comments.

2 Comments

Interesting info about HttpNotFoundHandler. Thanks!
Yes I understand that you can do this, however it detaches the html from the JavaScript, make it hard to maintain at a later stage hence the reason for not doing this. At present the JavaScript is inside the html view, so moving it to yet another directory just adds to the complexity.
1

JavaScript files by convention should be placed in project's Scripts folder and could be linked as

<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>" 
answered Sep 25, 2009 at 8:52

Comments

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.