网页中的 RoutePrefix api
RoutePrefix in web api
我使用 .net framework 4.0 和 web api 1.
我需要使用 RoutePrefix 属性,它位于 web api 2.
第一网有模拟吗api?
在网络中 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);
}
}
}
我的网站Api配置:
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 }
);
}
}
我的angular工厂:
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' } }
});
我需要更改什么来修复它?
In WebApiConfig which Route I need to add to get the FileController
by 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 }
);
}
}
在路由 table 中注册路由的顺序很重要,因为第一个匹配的路由获胜。在更一般的路由之前先注册更具体的路由以避免错误匹配。
我使用 .net framework 4.0 和 web api 1.
我需要使用 RoutePrefix 属性,它位于 web api 2.
第一网有模拟吗api?
在网络中 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);
}
}
}
我的网站Api配置:
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 }
);
}
}
我的angular工厂:
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' } }
});
我需要更改什么来修复它?
In WebApiConfig which Route I need to add to get the FileController by 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 }
);
}
}
在路由 table 中注册路由的顺序很重要,因为第一个匹配的路由获胜。在更一般的路由之前先注册更具体的路由以避免错误匹配。