放在哪里查看特定的JavaScript文件在ASP.NET MVC应用程序? [英] Where to put view-specific javascript files in an ASP.NET MVC application?

查看:113
本文介绍了放在哪里查看特定的JavaScript文件在ASP.NET MVC应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是最好的地方(哪个文件夹等),把视图特定的JavaScript文件在ASP.NET MVC应用程序?

What is the best place (which folder, etc) to put view-specific javascript files in an ASP.NET MVC application?

要保留我的组织的项目,我真的很想能够把它们并排侧视图的.aspx文件,但我还没有找到引用它们的好方法做的,没有暴露的时候〜/查看/动作/文件夹结构。难道真的是一件坏事,让该文件夹结构泄漏的细节?

To keep my project organized, I'd really love to be able to put them side-by-side with the view's .aspx files, but I haven't found a good way to reference them when doing that without exposing the ~/Views/Action/ folder structure. Is it really a bad thing to let details of that folder structure leak?

另一种方法是把它们在〜/脚本或者〜/文件夹的内容,但它是一个小的刺激,因为我现在担心的文件名冲突。这是一个刺激,我可以得到了,但是,如果是正确的事情。

The alternative is to put them in the ~/Scripts or ~/Content folders, but is a minor irritation because now I have to worry about filename clashes. It's an irritation I can get over, though, if it is "the right thing."

推荐答案

老问题,但我希望把我的回答柜面任何人来寻找它。

Old question, but I wanted to put my answer incase anyone else comes looking for it.

我也想我的观点的具体JS / CSS文件的意见文件夹下,这里是我是如何做到的:

I too wanted my view specific js/css files under the views folder, and here's how I did it:

在你需要修改两个部分,以使网络服务器提供文件/视图的根在web.config文件夹:

In the web.config folder in the root of /Views you need to modify two sections to enable the webserver to serve the files:

    <system.web>
        <httpHandlers>
            <add path="*.js" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
            <add path="*.css" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
            <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
        </httpHandlers>
        <!-- other content here -->
    </system.web>

    <system.webServer>
        <handlers>
            <remove name="BlockViewHandler"/>
            <add name="JavaScript" path="*.js" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
            <add name="CSS" path="*.css" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
            <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
        </handlers>
        <!-- other content here -->
    </system.webServer>

然后从你的视图文件,你可以参考的URL像你期望:

Then from your view file you can reference the urls like you expect:

@Url.Content("~/Views/<ControllerName>/somefile.css")

这将使的.js和.css文件的服务,并且将禁止别的服务。

This will allow serving of .js and .css files, and will forbid serving of anything else.

这篇关于放在哪里查看特定的JavaScript文件在ASP.NET MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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