【发布时间】:2016-06-20 03:17:47
【问题描述】:
我似乎无法使用 ng-file 上传。 我需要在我的控制器中使用网桥 ID 很好地传递
我得到的错误是:
"{"Message":"No HTTP resource was found that matches the request URI
'http://localhost/api/BridgeController/'.","MessageDetail":"No type was
found that matches the controller named 'BridgeController'."}"
无法弄清楚为什么它找不到我的方法。有什么想法吗?
这是我的控制器代码,它将被移动到服务中
$scope.uploadFile = function (file) {
console.log("hitting file upload", $scope.selectedBridge);
if (file) {
debugger;
var request = {
method: 'POST',
url: '/api/Bridge/UploadBridgeImage',
data: angular.toJson($scope.selectedBridge.BridgeID),
file: file,
headers: { 'Content-Type': undefined }
};
Upload.upload(request).then(function (response) {
});
}
}
和后端 C#
[HttpPost]
[Route("api/Bridge/UploadBridgeImage")]
public IHttpActionResult UploadBridgeImage()
{
try
{
var uploadedFiles = HttpContext.Current.Request.Files;
for (int i = 0; i < uploadedFiles.Count; i++)
{
var fileToSave = uploadedFiles[i];
var fileBytes = _iStremHelper.GetBytes(fileToSave.InputStream, fileToSave.ContentLength);
var file = BridgeFileEntity(fileBytes, fileToSave, 1);
using (_iFileRepository)
{
_iFileRepository.Save(file);
}
}
return Ok();
}
catch (Exception ex)
{
return InternalServerError();
}
}
在此处编辑代码。 我能够在我的 C# 控制器中的那个 post 方法中达到我的断点。文件有我需要的所有数据。现在我需要以某种方式获取“数据”。知道它在上下文中的位置吗?
【问题讨论】:
-
web api 中是否启用了属性路由?
-
尝试在客户端和路由属性中将路由重命名为“api/Bridge/UploadBridgeImage”。默认方式是不使用后缀控制器命名控制器。
-
@AlekseyL。是的,他们启用了 config.MapHttpAttributeRoutes();
-
@MarcusH 尝试了您的建议,得到相同的错误消息。
-
@kkdeveloper7 其他路线有效吗?你确定你的开发端口是80? (也许localhost:portNumber)
标签: c# angularjs asp.net-web-api