MVC View File and JavaScript

When inside the view, in MVC, and you have your JavaScript, I tried to move it to a JavaScript file in the same directory and it says that the javascript file cannot

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

Here is the problem: I need to save the JavaScript file side by side, if not in the same directory in the project as DinnerForm.aspx, how should I do this?

I don't want to post the full path like: "~ \ views \ dinners \ dinnerform.js" as this is just a job and causes problems later

Any ideas?

+2


source to share


2 answers


JavaScript files by convention must be placed in the scripts folder of the project and can be linked like



<script src="<%= Url.Content("~/Scripts/jquery-1.3.2.js") %>"  

      

+1


source


You cannot have JavaScript files in the Views folder because it doesn't actually exist in the browser. If you look at the Web.config file in the Views folder, you can see that it is installed in the handler HttpNotFoundHandler

. This means that it returns 404 for all requests to its folder.

Why not put them in the Scripts folder? You can always have a subfolder.



eg. /Scripts/Dinner/dinnerform.js

+4


source







All Articles