MVC在某些路由上忽略了.jpg文件扩展名-使用Controller操作? [英] MVC ignoring .jpg file extension on certain route -- Use Controller action?

查看:74
本文介绍了MVC在某些路由上忽略了.jpg文件扩展名-使用Controller操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从特定的网址系统提供图片文件,如下所示:

I'm trying to serve image files from a specific url system, like so:

mysite/Images/Category/File_Name.extension

mysite/Images/Category/File_Name.extension

我已经设置了一条这样的路线:

I've set up a route like so:

routes.MapRoute( "ImagesRoute", // Route name 
                 "Images/{category}/{file}.jpg", // URL with parameters 
                 new { controller = "Posts", 
                       action = "ViewImage", 
                       category = "", file = "" } // Parameter defaults 
                );

应该映射到我的控制器动作:

Which is supposed to map to my controller action:

public ActionResult ViewImage(string category, string file)
    {
        var dir = Server.MapPath("/Images");
        var imgtitle = file.Replace("_", " ").Replace(".jpg", "");
        var repos = new BlogImagesRepository();
        var guid = repos.FetchImageByCategoryAndTitle(category, imgtitle);
        var path = Path.Combine(dir, category, guid.ImageGuid.ToString());
        return File(path, "image/jpeg");
    }

如果我从路由中删除了.jpg扩展名,并要求在URL上没有.jpg扩展名的文件标题(即Images/MyCategory/My_Image),则显示的很好. 但是,添加.jpg扩展名会导致404错误-

If I remove the .jpg extension from the route and request a file title without the .jpg extension on the url (ie: Images/MyCategory/My_Image) it displays just fine. However, adding the .jpg extension results in a 404 error--

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

我认为它正在查找文件,而不是控制器操作.

I assume it's looking for the file, instead of the controller action.

在路由中添加.jpg并不能解决该问题;我不确定该怎么做,而我在Google上遇到的类似问题对我的运气还不是很高.

Adding the .jpg to the routes did not resolve this; I'm unsure what to do, and my google luck hasn't been very high with similar questions I saw on here.

如何为.jpg和类似图像类型执行此操作?我是否必须以某种方式为该特定路径设置忽略?如果是这样,我该怎么做?该应用程序目前仅在VS 2012中运行.

How can I do this for .jpg, and similar image types? Do I have to set up an ignore for that particular path somehow? If so, how do I go about doing that? The application is just being ran through VS 2012 currently.

推荐答案

您是正确的,因为对.jpg的请求不会发送到Controller并由IIS的静态文件处理程序处理.您实际上并不需要忽略,而是要将其传递给ASP.NET运行时.您会找到的大多数互联网搜索都会告诉您添加RunAllManagedModulesForAllRequests 或与此相关的内容;但是,这会影响性能,因此最简单的方法可能是在您的Web配置中添加另一个TransferRequestHandler,例如下面的 MyJpgHandler 示例:

You're right in that requests for .jpg aren't being sent through to your Controller and handled by the static file handler of IIS. You don't really want an ignore, but you want to pass it through to the ASP.NET runtime. Most of the internet searches you'll find will tell you to add RunAllManagedModulesForAllRequests or something to that respect; however, this has a performance impact so the easiest way to do this is probably to add another TransferRequestHandler in your web config, like the MyJpgHandler example below:

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />

    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_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.NET\Framework64\v4.0.30319\aspnet_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" />    

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

      <add name="MiniProfiler" path="mini-profiler-resources/*" verb="*" type="System.Web.Routing.UrlRoutingModule" resourceType="Unspecified" preCondition="integratedMode" />
    </handlers>
  </system.webServer>

这应该会导致您期望的行为(假设您也没有任何要用作同一URL路径中静态内容的.jpg文件)

This should result in behaviour you're expecting (assuming you don't also have any .jpg files that are meant to be served as static content from the same url path)

这篇关于MVC在某些路由上忽略了.jpg文件扩展名-使用Controller操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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