ASP.MVC 中的忽略路由

发布于 2024-08-12 08:18:47 字数 610 浏览 9 评论 0原文

我正在尝试访问视图目录中的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凌乱心跳 2024-08-19 08:18:47

Views 文件夹非常适合视图,而 javascript 应该放在其他地方。这就是为什么 MVC 框架的设计者将 web.config 放入此 Views 文件夹中,拒绝访问其中的任何文件。您可以修改此默认设置,但请注意,这可能是一个潜在的安全漏洞。因此,打开位于 Views 文件夹中的 web.config 文件,然后:

将:替换

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

为:

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

导航到 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 a web.config in this Views 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 the web.config file located in the Views folder and:

Replace:

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

with:

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

Navigate to http://yoursite/Views/test.js

P.S. You could also remove all the IgnoreRoutes you put in global.asax.

渡你暖光 2024-08-19 08:18:47

使用 DefaultHttpHandler 这不是一个更好的解决方案吗
对于 html 资源并为所有其他类型的文件保留 HttpNotFoundHandler

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

Wouldn't this be a better solution using the DefaultHttpHandler
for html resources and keeping the HttpNotFoundHandler for all other types of file

<httpHandlers>  
  <add path="*.html" verb="*" type="System.Web.DefaultHttpHandler"/> 
  <add path="*.*" verb="*" type="System.Web.HttpNotFoundHandler"/> 
</httpHandlers> 
呆萌少年 2024-08-19 08:18:47

实际上对于IIS集成模式,您需要使用System.Web.StaticHttpHandler

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

显然System.Web.DefaultHttpHandler仅适用于IIS经典模式。

Actually for IIS integrated mode, you need to use System.Web.StaticHttpHandler:

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

Apparently System.Web.DefaultHttpHandler works in IIS classic mode only.

会发光的星星闪亮亮i 2024-08-19 08:18:47

在 MVC 4 中,我必须更新处理程序部分以及 httpHandlers 部分。

我使用以下内容更新了 Views 文件夹中的 web.config。

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

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

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.

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

<handlers>
  <remove name="BlockViewHandler" />
  <add name="BlockViewHandlerRazor" path="*.cshtml" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
  <add name="BlockViewHandlerAspx" path="*.aspx" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文