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?

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

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") %>"  

Comments

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.