如何引用ASP.NET MVC 2视图文件夹中的javascript文件? [英] How to reference javascript file in ASP.NET MVC 2 Views folder?

查看:103
本文介绍了如何引用ASP.NET MVC 2视图文件夹中的javascript文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试适应公司标准,其中所有页面级javascript都包含在外部文件中,而不包含在ASPX或ASCX文件中.使用ASP.NET MVC 2,我想知道如何引用相对于视图的脚本文件(.js).

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.

换句话说,我不想用应用程序中将存在的所有小.js文件来污染我的Scripts文件夹.从组织的角度来看,将.js文件与.aspx/.ascx文件一起使用是最有意义的.例如,如果我有一个名为"MyController"的控制器,其动作名为"MyAction",则我在Views/MyController文件夹中有一个MyAction.aspx文件,并希望将名为"MyAction.js"的脚本文件放置在相同的文件夹.但是,我不能使用:

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>

看来这也不起作用:

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

所以我猜测问题可能与路由表有关,但我仍然是MVC新手,所以不确定.

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.

第二个问题,是否可以像传统ASP.NET一样将javascript嵌入为资源,并在我们的客户端页面中生成非人类可读的url?

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?

更新

我只能假设这个问题已经过时,这就是为什么没有其他人提供任何答案的原因.我不敢相信这是一个难题.因此,我想我应该添加此小更新,以查看是否可以恢复对话.

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.

推荐答案

您说对了,这是因为MVC路由.请记住,如果公开Views文件夹以包含JavaScript文件,它还将公开您的Views(虽然不应该,但是可能包含一些前端逻辑).

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).

我个人将使用scripts文件夹并在其中创建子文件夹.

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

可能希望在这里查看: ASP. NET MVC路由和静态数据(即图像,脚本等),了解ASP.NET MVC如何直接访问文件,请参见:

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.

如果Views文件夹中有一个web.config,并且按照第二个链接中所述的方式进行了配置,则对Views文件夹的所有外部请求都将返回404.

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: 您应该只允许通过添加以下内容来允许访问JavaScript:

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>

转到Views文件夹中的web.config.如您所见,我不确定处理程序将允许直接访问的类型.继续添加没有处理程序类型的代码,当您尝试访问.js文件时,您会看到它正在被调用

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

这篇关于如何引用ASP.NET MVC 2视图文件夹中的javascript文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆