C# Web API 2-错误:“1”;未找到与名为';调整大小'&引用;
我正试图用WebAPI做一个简单的文件上传API 这是控制器:C# Web API 2-错误:“1”;未找到与名为';调整大小'&引用;,c#,asp.net,asp.net-web-api,asp.net-web-api2,C#,Asp.net,Asp.net Web Api,Asp.net Web Api2,我正试图用WebAPI做一个简单的文件上传API 这是控制器: [RoutePrefix("api/resize")] public class ResizeController : ApiController { [HttpPost, Route("api/resize/preserveAspectRatio")] public async Task<IHttpActionResult> resizePreserveAspectRatio() {
[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();
}
}
当我与邮递员一起发布文件时,出现以下错误:
{
"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/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
}
}
Reference您的路由前缀已经显示“api/resize/”,那么在控制器路由中,您无需再次提及它。只需将“PreserveSpectratio”保留为@PM即可。提到,您当前的路由是
api/resize/api/resize/preserveSpectratio
,谢谢-嗯,我刚刚将路由更改为preserveSpectratio
,前缀路由更改为api/resize
,它仍然给出相同的错误。有什么想法吗@PM@zaitsmanThanks@Nkosi-刚刚尝试了你的第三个选项,[RoutePrefix(“api/resize”)]
和[HttpPost,Route(“~/api/resize/preserveAspectRatio”)]
,我得到了同样的错误。有什么想法吗?@Abr尝试第二种选择,看看是否有帮助。可能是覆盖与前缀相同的冲突。Hmm也尝试了@Nkosi、[RoutePrefix(“api/resize”)]
和[HttpPost,Route(“preserveAspectRatio”)]
并且得到了相同的错误,“MessageDetail”:“未找到与名为“resize”的控制器匹配的类型。”
@Abr基于您所展示的内容和建议的解决方案。这应该行得通。除非你没有向我们展示导致冲突的其他东西。这个项目好坏参半。MVC和WebAPI?@Abr为了测试这一理论,创建了另一个简单的路径,一个GET,看看你是否能找到它。
//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
}
}