0

enter image description hereI am trying to access the scripts which are placed within the folder scripts created within Areas. But whenever i am trying to run the View, i am getting an error 404 saying the file was not found.

  <script src="/Areas/TestAreas/Scripts/AutoPostBack.js" type="text/javascript"></script>
Ashish Babu
  • 1,167
  • 9
  • 20
  • Where is the script located in your solution and what url the browser is trying to access and getting a 404 ? – Shyju Nov 19 '17 at 14:45
  • @Shyju, I have created an area in my mvc project. I have created an area "TestAreas". Within this Area, I have created a folder named as Scripts. The folder scripts contains the js file(AutoPostBack.js). I have added the reference to the js files but still it could not find the file. – Ashish Babu Nov 19 '17 at 14:48
  • I could see it in developer tools under network tag. The file AutoPostBack.js could not be found. error 404 – Ashish Babu Nov 19 '17 at 14:50
  • The view is getting displayed, but the js files are not getting loaded – Ashish Babu Nov 19 '17 at 14:50
  • 1
    Which file are you trying to include the script ? – Shyju Nov 19 '17 at 14:51
  • when my view is getting loaded, i want the page to be posted back. for which i have added the script AutoPostBack,js. if i could get the script executed on load of my page, then my work is done. just need my script file to be rendered along with my view. – Ashish Babu Nov 19 '17 at 14:54
  • 1
    You still did not answer the question. What is the url your browser is trying to call ? – Shyju Nov 19 '17 at 15:15
  • URL:http://localhost:50421/TestArea/Home/ – Ashish Babu Nov 19 '17 at 15:29

1 Answers1

0

You should include a ~ prefix when building the url to your file.

When razor sees ~/ It knows it has to get the file from the app root, So it will look for the remaining part(Areas/TestAreas/Scripts/AutoPostBack.js) from the app root .

This should work

<script src="~/Areas/TestAreas/Scripts/AutoPostBack.js" type="text/javascript"></script>
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • When you use this, your browser still gives you a 404 ? What is the path your browser is requesting for this file ? (right click on the network tab and open in new tab and copy the url) – Shyju Nov 19 '17 at 15:32
  • I am loading a view. In the View page, i have added an reference for js file.Browser is not throwing an error 404. My js files are not getting executed which should have been, when the view gets loaded. When i go into developer tool of chrome, i can see under the network tab , that it is not able to find my js file. An error 404 is displayed there – Ashish Babu Nov 19 '17 at 15:41
  • You still did not answer the question i asked. When you say "it is not working" , are you saying that you are still getting a 404 response ? If yes ,what url it is your browser trying to load ? – Shyju Nov 19 '17 at 15:43
  • Let me put it in simple words. How can i access js files which are kept in areas. The script code you mentioned above is not able to access the js files. – Ashish Babu Nov 19 '17 at 16:16
  • it should work as long as the file exist in that location. What version of mvc are you using ? – Shyju Nov 19 '17 at 16:27
  • MVC 3. i think the web.config files present in the View folder within the areas blocks the files from rendering – Ashish Babu Nov 19 '17 at 16:46
  • but your script is not inside the view folder.This works fine in MVC5. – Shyju Nov 19 '17 at 16:47
  • Even if i keep it there, it is showing the same error – Ashish Babu Nov 19 '17 at 17:03