MVC - 我可以使用URL扩展公开操作方法吗?

时间:2016-01-19 20:05:12

标签: asp.net asp.net-mvc asp.net-mvc-4

我该怎样做?

我希望用户浏览到https://localhost/Registration/GetCaptchaAudioInternetExplorer.wav并让其在GetCaptchaAudioInternetExplorer控制器上运行Registration,该控制器提供audio/wav文件。

现在对我有用的是浏览https://localhost/Registration/GetCaptchaAudioInternetExplorer

但是我需要做些什么来使https://localhost/Registration/GetCaptchaAudioInternetExplorer.wav路由到同一个动作?在MVC中有没有办法为这样的事情指定行动路线?

2 个答案:

答案 0 :(得分:1)

您可以使用URL Rewrite用于IIS(7+)来执行此操作,基本上:

<rule name="Rewrite Wav Files" stopProcessing="true">
  <match url="^Registration/?(.*)\.wav$" />
  <action type="Rewrite" url="/Registration/{R:1}" />
</rule>

这将剥离扩展并将其发送到控制器。它已被重写,因此它仍应在浏览器中显示为 Registration / GetCaptchaAudioInternetExplorer.wav

您可以尝试设置relaxedUrlToFileSystemMapping

<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true" />

但是.wav是一个真实的东西,我不确定这是否会奏效。关于Haacked的更多细节。

<小时/> 最后的替代方案是enable RAMMFAR

  

RAMMFAR表示&#34; runAllManagedModulesForAllRequests&#34;并指的是这个   web.config中的可选设置。

 <system.webServer>   
     <modules runAllManagedModulesForAllRequests="true" />    
 </system.webServer>

无论扩展名如何,都应通过MVC发送所有请求。我说最终,因为它有性能影响。

答案 1 :(得分:0)

一点点衍生,但我能够将https://localhost/Registration/GetCaptchaAudioInternetExplorer/clip.wav路由到我的GetCaptchaAudioInternetExplorer行动。

两个简单的改变:

1)将此路线添加到您的行动中:

[Route("clip.wav")]
public async Task<ActionResult> GetCaptchaAudioInternetExplorer()

2)将此处理程序添加到web.config

<system.webServer><handlers><add name="Wave File Handler" path="clip.wav" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />