ASP.NET MVC - 如果你不想将它们存储在/ Scripts,你把.js文件放在哪里? [英] ASP.NET MVC - Where do you put your .js files if you dont want to store them in /Scripts?

查看:268
本文介绍了ASP.NET MVC - 如果你不想将它们存储在/ Scripts,你把.js文件放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些.js文件,我想存储在相同的目录中,他们的意见(他们是特定于一个视图 - 它只是保持javascript与视图的HTML分开)



但是,将它们添加到 / Views / ControllerName / 目录不起作用,因为当向Web服务器发出请求时。 js文件:

 < script type =text / javascriptsrc =/ Views / ControllerName / myscript.js> ;< / script> 

它本质上是指向'Views'控制器,显然不存在。



更新



根据我已阅读的内容,在global.asax.cs中添加以下IgnoreRoute RegisterRoutes方法应该允许访问任何请求的.js文件:

  routes.IgnoreRoute({resource} .js / {* pathInfo }); 

但是,我似乎无法让它工作吗?



在找到并测试了多个帖子后(最确定的后此处



Global.asax 中,将以下代码添加到 RegisterRoutes 方法中:

  routes.IgnoreRoute {file} .js); 

这里有一个很棒的文章描述这个和附加活动这里


I have a number of .js files that I would like to be stored in the same directories as their views (they're specific to a view - its simply to keep the javascript separate from the view's HTML)

However, adding them to the /Views/ControllerName/ directory wont work because when a request is made to the webserver for the .js file:

<script type="text/javascript" src="/Views/ControllerName/myscript.js"></script>

It would essentially be directed at the 'Views' controller which obviously doesnt exist.

Update

From what I have read, adding the following IgnoreRoute in the global.asax.cs RegisterRoutes method should permit access to any requested .js file:

routes.IgnoreRoute("{resource}.js/{*pathInfo}");

However, I cant seem to get it to work?

解决方案

SOLVED

After having found and tested a number of posts (most conclusive post here) that I couldnt get to work, the following solves the puzzle:

In the Global.asax, add the following code to the RegisterRoutes method:

routes.IgnoreRoute("{file}.js");

There is a great post here that describes this and additional activity here

这篇关于ASP.NET MVC - 如果你不想将它们存储在/ Scripts,你把.js文件放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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