ASP.NET MVC - 路由 - 带有文件扩展名的操作 [英] ASP.NET MVC - Routing - an action with file extension

查看:32
本文介绍了ASP.NET MVC - 路由 - 带有文件扩展名的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法实现调用URL http://mywebsite/myarea/mycontroller/myaction.xml这基本上是假"请求文件,但结果将是一个动作操作,为动态创建的文件提供服务?

is there a way to achieve calling URL http://mywebsite/myarea/mycontroller/myaction.xml This would basically "fake" requesting a file but the result would be an action operation that would serve a file created dynamically?

我试过了:

context.MapRoute(
                "Xml_filename",
                "Xml/{controller}/{action}.xml"
            );

但是每当 URL 中存在文件扩展名时,路由就会失败并表现得像我直接请求文件一样.

but whenever there is a filextension in the URL the routing fails and behaves as I was requesting a file directly.

我怀疑这可能是因为使用了无扩展名的 url 处理程序.

I suspect this might be because of using extension less url handler.

<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFrameworkv4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
        <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%Microsoft.NETFramework64v4.0.30319aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

感谢您的任何建议.

雅库布

推荐答案

您需要将 XML 文件的请求映射到 web.config 中的 TransferRequestHandler.否则 IIS 将处理请求.

You need to map requests for your XML files to TransferRequestHandler in web.config. Otherwise IIS will handle the request.

Jon Galloway 解释了如何做到这一点 此处.

Jon Galloway explains how to do this here.

总而言之,您将此元素添加到 web.config 中的 location/system.webServer/handlers:

In summary, you add this element to location/system.webServer/handlers in your web.config:

<add name="XmlFileHandler" path="*.xml" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />

这篇关于ASP.NET MVC - 路由 - 带有文件扩展名的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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