Web API中没有命中自定义路由

时间:2017-06-09 12:13:22

标签: c# asp.net .net asp.net-web-api asp.net-web-api2

我的Web API项目中的控制器中有以下操作。

 [System.Web.Http.RoutePrefix("api/Events")]
public class EventsController : ApiController
{
    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("Images/{title}/{id}/{photoName}")]
    public HttpResponseMessage GetImage(string title, Guid id, string photoName)
    {
        var path = $"c:\\Photos\\{title}\\{id}\\{photoName}";
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
        var stream = new FileStream(path, FileMode.Open, FileAccess.Read);
        result.Content = new StreamContent(stream);
        result.Content.Headers.ContentType =
            new MediaTypeHeaderValue("application/octet-stream");
        return result;
    }
}

这是我的WebApiConfig课程。

public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }

我在我的这个动作中设置了一个断点。问题是,断点没有被击中。

以下是我使用的网址:

  

http://localhost:52584/api/Events/Images/Test_Title/80b88b32-0e45-4b8f-9a63-61ee28bd11fd/Test.jpg

是否有可能断点没有被击中,因为我正在传递.jpg扩展名?

1 个答案:

答案 0 :(得分:1)

With reference to this accepted answer, I could solve my issue with the following handler:

<system.webServer>
  <handlers>
    <add 
      name="ManageJPGHandler" 
      path="api/Events/Images/*/*/*.jpg" 
      verb="GET" 
      type="System.Web.Handlers.TransferRequestHandler" 
      preCondition="integratedMode,runtimeVersionv4.0"
    />
  </handlers>
</system.webServer>