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

Deadly 提交于 2019-12-12 13:15:36

问题


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.


回答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




回答2:


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.



来源:https://stackoverflow.com/questions/5857265/how-to-reference-javascript-file-in-asp-net-mvc-2-views-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!