【问题标题】:Custom routes not being hit in Web API [duplicate]Web API中未命中自定义路由[重复]
【发布时间】:2017-06-09 12:13:22
【问题描述】:

我在我的 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 扩展名而导致断点未命中?

【问题讨论】:

  • 请将您的 webapi 配置添加到此问题中。
  • 更新了我的问题以包含 WebApiConfig
  • 这不是你的 webapi 配置。我想你遇到了这个问题。第二个答案对我们有好处。 stackoverflow.com/questions/14664488/…
  • 如果你去掉文件扩展名,你打吗?
  • 这是导致问题的文件扩展名。

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


【解决方案1】:

参考this 接受的答案,我可以使用以下处理程序解决我的问题:

<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>

【讨论】:

  • 很高兴您了解了它。必须履行我的公民义务并杀死你的问题。对不起。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2012-12-29
  • 1970-01-01
相关资源
最近更新 更多