将特定于视图的 javascript 文件放在 ASP.NET MVC 应用程序中的何处? [英] Where to put view-specific javascript files in an ASP.NET MVC application?

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

问题描述

在 ASP.NET MVC 应用程序中放置特定于视图的 javascript 文件的最佳位置(哪个文件夹等)是什么?

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

为了让我的项目井井有条,我真的很想将它们与视图的 .aspx 文件并排放置,但我还没有找到在这样做时引用它们而不暴露它们的好方法~/Views/Action/文件夹结构.让那个文件夹结构的细节泄露真的是一件坏事吗?

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?

另一种方法是将它们放在 ~/Scripts 或 ~/Content 文件夹中,但有点烦人,因为现在我不得不担心文件名冲突.不过,如果这是正确的事情",我可以克服这种恼怒.

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.

我也想在 views 文件夹下查看特定于我的 js/css 文件,我是这样做的:

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

在/Views 根目录下的 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>

然后从您的视图文件中,您可以像您期望的那样引用网址:

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天全站免登陆