我使用 .net framework 4.0 和 web api 1 。
我需要使用属性 RoutePrefix ,它位于 web api 2 中。
第一个网络API是否有模拟?
在Web Api 2中:
[RoutePrefix("api/file")]
public class FileController : ApiController
{
...
我的Api控制器:
public class FileController : ApiController
{
private IFileManager fileManager;
public FileController()
: this(new LocalFileManager(HttpRuntime.AppDomainAppPath + @"\Album"))
{
}
public FileController(IFileManager fileManager)
{
this.fileManager = fileManager;
}
// GET: api/Photo
[HttpGet]
public async Task<HttpResponseMessage> Get()
{
var results = await fileManager.Get();
return Request.CreateResponse(HttpStatusCode.OK, new { photos = results });// (new { photos = results });
}
// POST: api/Photo
[HttpPost]
public async Task<HttpResponseMessage> Post()
{
// Check if the request contains multipart/form-data.
if (!Request.Content.IsMimeMultipartContent("form-data"))
{
return Request.CreateResponse(HttpStatusCode.BadRequest, "Unsupported media type");// BadRequest("Unsupported media type");
}
try
{
var photos = await fileManager.Add(Request);
return Request.CreateResponse(HttpStatusCode.OK, new { Message = "Photos uploaded ok", Photos = photos });// Ok(new { Message = "Photos uploaded ok", Photos = photos });
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.BadRequest, ex.GetBaseException().Message);// BadRequest(ex.GetBaseException().Message);
}
}
// DELETE: api/Photo/5
[HttpDelete]
[ActionName("{fileName}")]
public async Task<HttpResponseMessage> Delete(string fileName)
{
if (!this.fileManager.FileExists(fileName))
{
return Request.CreateResponse(HttpStatusCode.NotFound);// NotFound();
}
var result = await this.fileManager.Delete(fileName);
if (result.Successful)
{
return Request.CreateResponse(HttpStatusCode.OK, new { message = result.Message });// Ok(new { message = result.Message });
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest, result.Message);// BadRequest(result.Message);
}
}
}
我的WebApiConfig:
public static class WebApiConfig
{
//public static JsonSerializerSettings JsonSerializerSettings { get; private set; }
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "FileApi",
routeTemplate: "api/file/{action}/{fileName}",
defaults: new { controller = "File", action = "Get", id = RouteParameter.Optional }
);
}
}
我的角度工厂:
function fileManagerClient($resource, $http) {
return $resource("api/file/:fileName",
{ fileName: "@fileName" },
{
'query': { method: 'GET' },
'save': { method: 'POST', transformRequest: angular.identity, headers: { 'Content-Type': undefined } },
'remove': { method: 'DELETE', url: 'api/file/:fileName', params: { name: '@fileName' } }
});
我需要改变什么来修复它?
答案 0 :(得分:0)
在WebApiConfig中,我需要添加Route以获取FileController 通过api / file?
使用api/{controller}
的基于约定的通用默认路由应该有效。如果要定位该控制器,您仍然可以创建一个特定于文件控制器。
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Convention-based routing.
//GET api/file
//POST api/file
//DELETE api/file/somefilename
config.Routes.MapHttpRoute(
name: "FileApi",
routeTemplate: "api/file/{fileName}",
defaults: new { controller = "File", fileName = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
在第一个匹配的路线获胜时,在路线表中注册路线的顺序非常重要。在更多一般路线之前首先注册更具体的路线,以避免错误匹配。