ASP.MVC 中的忽略路由
我正在尝试访问视图目录中的 .js 文件。 我有一个带有 /Views/Home/MyControl.ascx 的 MVC 应用程序 我有一个 js 文件 /Views/Home/MyControl.js
我希望引用 .js 文件并将其与控件一起保存。 我已经在路由中尝试了以下条目,但似乎都不起作用。
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{controller}/{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{*alljs}", new { alljs = @".*\.js(/.*)?" });
请帮忙,请不要建议将 .js 文件添加到脚本目录中。 我想让它以这种方式工作,或者知道为什么它不能完成。
我将脚本放入页面,只是脚本调试在VS2010 B2中被破坏。
谢谢 问候 克雷格.
I am trying to access a .js file in the views directory.
I have an MVC application with /Views/Home/MyControl.ascx
I have a js file /Views/Home/MyControl.js
I wish to reference the .js file and keep it with the control.
I have tried the following entries in the routing, and none seem to work.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{file}.js");
routes.IgnoreRoute("{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{controller}/{resource}.js/{*pathInfo}");
routes.IgnoreRoute("{*alljs}", new { alljs = @".*\.js(/.*)?" });
Please help, please don't suggest adding the .js file to the scripts directory.
I would like to make it work this way, or know why it cannot be done.
I would put the script into the page, only script debugging is broken in VS2010 B2.
Thanks
Regards
Craig.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Views
文件夹非常适合视图,而 javascript 应该放在其他地方。这就是为什么 MVC 框架的设计者将web.config
放入此Views
文件夹中,拒绝访问其中的任何文件。您可以修改此默认设置,但请注意,这可能是一个潜在的安全漏洞。因此,打开位于Views
文件夹中的web.config
文件,然后:将:替换
为:
导航到
http://yoursite/Views/test.js
PS 您还可以删除放入
global.asax
中的所有IgnoreRoutes
。The
Views
folder is, well for views, and javascript should be put elsewhere. That's why the designers of the MVC framework put aweb.config
in thisViews
folder that denies access to any file inside. You could modify this defaut setting but be warned that this could be a potential security hole. So open theweb.config
file located in theViews
folder and:Replace:
with:
Navigate to
http://yoursite/Views/test.js
P.S. You could also remove all the
IgnoreRoutes
you put inglobal.asax
.使用 DefaultHttpHandler 这不是一个更好的解决方案吗
对于 html 资源并为所有其他类型的文件保留 HttpNotFoundHandler
Wouldn't this be a better solution using the DefaultHttpHandler
for html resources and keeping the HttpNotFoundHandler for all other types of file
实际上对于IIS集成模式,您需要使用
System.Web.StaticHttpHandler
:显然
System.Web.DefaultHttpHandler
仅适用于IIS经典模式。Actually for IIS integrated mode, you need to use
System.Web.StaticHttpHandler
:Apparently
System.Web.DefaultHttpHandler
works in IIS classic mode only.在 MVC 4 中,我必须更新处理程序部分以及 httpHandlers 部分。
我使用以下内容更新了 Views 文件夹中的 web.config。
In MVC 4 I had to update the handler section as well as the httpHandlers section.
I updated the web.config in the Views folder with the following.