Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC - Where do you put your .js files if you dont want to store them in /Scripts?

I have a number of .js files that I would like to be stored in the same directories as their views (they're specific to a view - its simply to keep the javascript separate from the view's HTML)

However, adding them to the /Views/ControllerName/ directory wont work because when a request is made to the webserver for the .js file:

<script type="text/javascript" src="/Views/ControllerName/myscript.js"></script>

It would essentially be directed at the 'Views' controller which obviously doesnt exist.

Update

From what I have read, adding the following IgnoreRoute in the global.asax.cs RegisterRoutes method should permit access to any requested .js file:

routes.IgnoreRoute("{resource}.js/{*pathInfo}");

However, I cant seem to get it to work?

like image 786
Jimbo Avatar asked Nov 06 '22 13:11

Jimbo


1 Answers

SOLVED

After having found and tested a number of posts (most conclusive post here) that I couldnt get to work, the following solves the puzzle:

In the Global.asax, add the following code to the RegisterRoutes method:

routes.IgnoreRoute("{file}.js");

There is a great post here that describes this and additional activity here

like image 65
Jimbo Avatar answered Nov 11 '22 15:11

Jimbo