【问题标题】:Web API 2 - Error: "No type was found that matches the controller named 'resize'."Web API 2 - 错误:“未找到与名为‘resize’的控制器匹配的类型。”
【发布时间】:2018-01-01 23:20:33
【问题描述】:

我正在尝试使用 Web API 做一个简单的文件上传 API。

这里是控制器:

[RoutePrefix("api/resize")]
public class ResizeController : ApiController
{

    [HttpPost, Route("api/resize/preserveAspectRatio")]
    public async Task<IHttpActionResult> resizePreserveAspectRatio()
    {
        if (!Request.Content.IsMimeMultipartContent())
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

        int maxWidth = 100;
        int maxHeight = 100;

        var provider = new MultipartMemoryStreamProvider();
        await Request.Content.ReadAsMultipartAsync(provider);
        foreach (var file in provider.Contents)
        {
            var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
            var buffer = await file.ReadAsByteArrayAsync();
            //Do whatever you want with filename and its binaray data.


        }

        return Ok();
    }


}

这是我的 WebApiConfig:

public static class 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 }
        );
    }
}

当我使用 PostMan 发布文件时,出现以下错误:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:26303/api/resize/preserveAspectRatio'.",
    "MessageDetail": "No type was found that matches the controller named 'resize'."
}

这不是骗人的 - 找不到其他文章来解决这个特定的组合。

【问题讨论】:

  • 您的路由前缀已经显示“api/resize/”,那么在您的控制器路由中,您无需再次提及它。只需保持“preserveAspectRatio”
  • 作为@PM。提到,您当前的路线是api/resize/api/resize/preserveAspectRatio
  • 谢谢 - 嗯,我刚刚将它更改为 preserveAspectRatio 用于路由,api/resize 用于前缀路由,它仍然给出相同的错误。有什么想法吗? @PM @zaitsman

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


【解决方案1】:

正如您所料,这是一个路由问题。 cmets 已经确定您与您的路由和路由前缀属性存在冲突,从而导致以下路由

api/resize/api/resize/preserveAspectRatio

映射到您的操作。

要获得所需的路由,您可以从控制器本身中删除前缀。

//removed prefix
public class ResizeController : ApiController {
    //Matches POST api/resize/preserveAspectRatio
    [HttpPost, Route("api/resize/preserveAspectRatio")]
    public async Task<IHttpActionResult> resizePreserveAspectRatio() {
        //...removed for brevity
    }
}

或者将它从方法上的路由中移除

[RoutePrefix("api/resize")]
public class ResizeController : ApiController {
    //Matches POST api/resize/preserveAspectRatio
    [HttpPost, Route("preserveAspectRatio")]
    public async Task<IHttpActionResult> resizePreserveAspectRatio() {
        //...removed for brevity
    }
}

或者通过在方法属性上使用波浪号 (~) 覆盖路由前缀

[RoutePrefix("api/resize")]
public class ResizeController : ApiController {
    //Matches POST api/resize/preserveAspectRatio
    [HttpPost, Route("~/api/resize/preserveAspectRatio")]
    public async Task<IHttpActionResult> resizePreserveAspectRatio() {
        //...removed for brevity
    }
}

参考Attribute Routing in ASP.NET Web API 2

【讨论】:

  • 谢谢@Nkosi - 刚刚尝试了您的第三个选项,[RoutePrefix("api/resize")][HttpPost, Route("~/api/resize/preserveAspectRatio")],我得到了同样的错误。有什么想法吗?
  • @Abr 尝试第二个选项,看看是否有帮助。可能是覆盖与前缀相同的冲突。
  • 嗯,@Nkosi、[RoutePrefix("api/resize")][HttpPost, Route("preserveAspectRatio")] 也试过了,我得到了同样的错误,"MessageDetail": "No type was found that matches the controller named 'resize'."
  • @Abr 根据您所展示的内容和建议的解决方案。那应该工作。除非您没有向我们展示导致冲突的其他原因。项目是否混杂。 MVC 和 Web API?
  • @Abr 来测试这个理论,创建另一个简单的路线,可能是 GET,看看你能不能打到它。
猜你喜欢
  • 1970-01-01
  • 2020-02-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-21
  • 2013-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多