How to link to javascript file in ASP.NET MVC 2 Views folder?

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

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

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

and it doesn't seem to work either:

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

So, I assume that the problem may be related to the routing table, but I'm still a newbie to MVC, so I'm not sure.

Second question: is it possible to embed javascript as a resource, as we can, using traditional ASP.NET and generate a non-human-readable URL on our client page?

UPDATE

, , . , . , , , , .

+3
2

, - MVC. , Views, JavaScript, (, , ).

.

Edit: : ASP.NET MVC- ( , ..) , ASP.NET MVC , : http://haacked.com/archive/2008/02/12/asp.net-mvc-blocking-direct-access-to-views.aspx , "" .

web.config Views , , Views 404.

Edit2: JavaScript :

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

web.config "". , , . , , , .js.

+1

, Views, Scripts, , . , , :

httpHandlers web.config :

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

:

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

. Javascripts :

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

.

0

All Articles