MVC路线行动为Javascript文件 [英] MVC Route to Action for Javascript file

查看:113
本文介绍了MVC路线行动为Javascript文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个MVC的途径来从控制器的JavaScript。我加入了以下路线,这是行不通的:

  routes.MapRouteWithName(
           DataSourceJS,//路线名称
           脚本/实体/ {}控制器/datasource.js,//带参数的URL
           新{控制器=家,行动=DataSourceJS} //参数默认,
           , 空值
           );

但是,如果我改变路线没有的.js,我定位到脚本/实体/ {}控制/数据源它的工作原理。但是,我需要有.js文件扩展名在那里,我该如何使这项工作?


解决方案

  

我如何使这项工作?


IIS截获该请求,因为它包含一个文件扩展名,并劫持它认为它是一个静态文件,而不是将它传递给你的应用程序。

要使它工作,你应该告诉IIS不这样做。在< system.webServer> 部分,您可以添加以下处理程序表明,与指定模式的请求应该由托管管道的处理方式:

 < system.webServer>
    <&处理GT;
        ...
        <添加名称=ScriptsHandler路径=脚本/实体/ * / datasource.js动词=GETTYPE =System.Web.Handlers.TransferRequestHandlerpreCondition =integratedMode,runtimeVersionv4.0/ >
    < /处理器>
< /system.webServer>

也有的人可能会告诉你使用:

 <模块runAllManagedModulesForAllRequests =真/>

但我不建议你在做是因为这意味着现在将流经托管管道这可能会对您的应用程序产生负面的性能开销静态资源的所有请求。处理程序语法允许您有选择地启用此仅适用于特定的路由模式和HTTP动词。

I am trying to add a mvc route to generate a javascript from the controller. I have added the following route and it doesn't work:

routes.MapRouteWithName(
           "DataSourceJS", // Route name
           "Scripts/Entities/{controller}/datasource.js", // URL with parameters
           new { controller = "Home", action = "DataSourceJS"} // Parameter defaults,
           , null
           );

But if I change the route to not have the ".js" and I navigate to "Scripts/Entities/{controller}/datasource" it works. But I need to have the .js file extension on there, how do I make this work?

解决方案

how do I make this work?

IIS intercepts the request because it contains a file extension and hijacks it thinking it is a static file and not passing it to your application.

To make it work you should tell IIS not to do that. Inside the <system.webServer> section you could add the following handler to indicate that requests with the specified pattern should be handled by the managed pipeline:

<system.webServer>
    <handlers>
        ...
        <add name="ScriptsHandler" path="Scripts/Entities/*/datasource.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

Some people might also tell you to use:

<modules runAllManagedModulesForAllRequests="true" />

but I wouldn't recommend you doing that because this means that all requests to static resources will now be flowing through the managed pipeline which could have a negative performance overhead for your application. The handler syntax allows you to selectively enable this only for certain route patterns and HTTP verbs.

这篇关于MVC路线行动为Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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