3

I am trying to accomodate a corporate standard where all page-level javascript is contained in an external file and not included within the ASPX or ASCX file. Using ASP.NET MVC 2, I'm wondering how I can reference the script file (.js) relative to the view.

In other words, I don't want to polute my Scripts folder with all of the small .js files that will exist in the application. From an organizational perspective, having the .js file alongside the .aspx/.ascx file makes the most sense. For example, if I have a controller named "MyController" with an action named "MyAction", I would have a MyAction.aspx file in the Views/MyController folder and would like to place the script file named "MyAction.js" in the same folder. However, I can't use:

<script src="MyAction.js" type="text/javascript"></script>

and it appears that this doesn't work either:

<script src="<%= Url.Content("~/Views/MyController/MyAction.js") %>"
        type="text/javascript"></script>

So I'm guessing the problem may have to do with the routing table, but I'm still a MVC newbie, so I'm not sure.

Second question, is it possible to embed the javascript as a resource like we can with traditional ASP.NET and generate the non-human-readable url in our client page?

UPDATE

I can only assume that this question has become stale which is why no one else has offered any answers. I can't believe it's that difficult of a question. So, I thought I'd add this little update to see if I can resuscitate the conversation.

SonOfPirate
  • 5,642
  • 3
  • 41
  • 97
  • I'm pretty sure you can use the routing to serve any *.js file referenced, but I don't have the time to check it out at the moment. Good luck! – Anders Arpi May 02 '11 at 13:29

2 Answers2

1

You're right it's because of MVC routing. Keep in mind that if you expose the Views folder to contain the JavaScript files it will also expose your Views (which although shouldn't, may contain some front-end logic).

Personally I would use the scripts folder and create subfolders there.

Edit: Might want to look here: ASP.NET MVC routing and static data (ie. images, scripts, etc) for how ASP.NET MVC accesses files directly, and here: http://haacked.com/archive/2008/02/12/asp.net-mvc-blocking-direct-access-to-views.aspx on why scripts in your Views folder might not be directly accessible.

If you have a web.config in you Views folder and it's configured the way described in the second link then all external requests to Views folder will return 404.

Edit2: You should be able to allow access to JavaScript only by adding:

<httpHandlers>
    <add path="*.js" verb="*" type="System.Web.StaticFileHandler"/>
    <add path="*" verb="*" type="System.Web.HttpNotFoundHandler" />
</httpHandlers>

To web.config in your Views folder. As you can see I'm not sure what type the handler would be to allow direct access. Go ahead and add it without a handler type and you'll see that it's being called when you try to access .js files

LukeP
  • 10,422
  • 6
  • 29
  • 48
  • Yea, you're right that I wouldn't want to break normal routing rules that prevent direct access to my aspx and ascx files. Can I just open up *.js files and Anders suggests? – SonOfPirate May 02 '11 at 13:36
  • I've been waiting to see if anyone else would chime in an help answer the question. Unfortunately "NOT_SURE_WHAT_TYPE" hasn't gotten me very far. – SonOfPirate May 12 '11 at 12:33
  • Sorry. I hoped that someone would chime in too. – LukeP May 12 '11 at 14:32
  • I've been told that you would need to write a custom Handler for that. – LukeP May 12 '11 at 14:42
  • 3
    You don't need a custom handler. It is System.Web.StaticFileHandler Check out this post for the details http://stackoverflow.com/questions/604883/where-to-put-view-specific-javascript-files-in-an-asp-net-mvc-application – Mr Bell Jun 23 '11 at 20:20
0

I also far prefer keeping my scripts that only pertain to a particular view inside the Views folder so that these scripts don't pollute my Scripts which I reserve for scripts that are accessed globally. The shortest solution to this problem that I have found is the following:

Change the httpHandlers in your web.config from this:

<httpHandlers>
  <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

to this:

<httpHandlers>
  <add path="*.cshtml" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

Thats it. Now you can put Javascripts inside your view folder and link them like this:

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

Thanks to Vadym Nikolaiev for this solution which he posted here.

Community
  • 1
  • 1
BruceHill
  • 6,954
  • 8
  • 62
  • 114