配售JS文件在views文件夹 [英] Placing js files in the views folder

查看:185
本文介绍了配售JS文件在views文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把我的JavaScript文件,我的看法。

I am trying to place my javascript files with my views.

我有以下的js文件的位置。
/Views/Home/Home.js

I have the following js file location. /Views/Home/Home.js

然而,当一个脚本标记引用时,它会导致一个404错误。

However, when referenced with a script tag, it results in a 404 error.

按以下SO问题:<一href=\"http://stackoverflow.com/questions/2865388/asp-net-mvc-where-do-you-put-your-js-files-if-you-dont-want-to-store-them-in\">ASP.NET MVC - 你在哪里把你的.js文件,如果你不想将它们存储在/脚本

我添加file.js我的注册路线。 (没解决问题)

I added file.js to my Register routes. (Did not resolve the problem)

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{file}.js");
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    }

我怎么可以存储并引用我的JS文件旁边,我的看法?

How can i store and reference my js files next to my views?

推荐答案

的问题是,出于安全原因,web.config中,它是浏览文件夹块内的所有请求,文件文件夹中。这是您将在配置文件中查找:

The problem is that, for security reasons the web.config that is inside the Views folder blocks all request to files in that folder. This is what you will find in the config file:

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

和IIS7的:


    

<handlers>
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

解决方案

您可以更改通配符只捕获的.cshtml文件。

You can change the wildcard to catch only the .cshtml files.

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


    

<handlers>
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>

通过这个通配符时,.js文件不会被阻止。

With this wildcard, the .js files won't be blocked.

这篇关于配售JS文件在views文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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