强制路由到我的 API
Forcing a Route to my API
我的 API 控制器 (MVC5) 中有这 2 个 api:
[HttpGet]
[HttpPost]
public RestErrorHandler Add([FromBody]Models.Customer customer)
{
try
{
//do something...
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
[HttpGet]
[HttpPost]
public RestErrorHandler Delete([FromBody]Models.Customer customer)
{
try
{
//do something
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
我的客户电话是这样的:
var response = client.PostAsync(string.Format("http://my_uri/api/Customer/" + action), contentPost).Result;
其中 'action' 可以是 'Add' 或 'Delete'。
测试删除它告诉我这个错误:
Multiple actions were found that match the request:
为了尝试 'force' 它以正确的方式运行,我在 API:
的方法中添加了 Route 标记
[HttpGet]
[HttpPost]
[Route("Customer/Delete/Customer")]
public RestErrorHandler Delete([FromBody]Models.Customer customer)
{
try
{
//do something
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
但是它调用我的 'Add' api..?
请问我错过了什么?
这两个方法都是POST的,去掉GET。
[RoutePrefix("api")]
public class YourController: ApiController
{
[HttpPost, Route("customers/add")]
public RestErrorHandler Add([FromBody]Models.Customer customer)
{
try
{
//do something...
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
[HttpPost, Route("customers/remove")]
public RestErrorHandler Delete([FromBody]Models.Customer customer)
{
try
{
//do something
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
}
public void Configuration(IAppBuilder app)
{
var configuration = new HttpConfiguration();
//for route attributes on controllers
configuration.MapHttpAttributeRoutes();
}
var response = await client.PostAsync("http://my_uri/api/customers/add", contentPost);
邮递员
我的 API 控制器 (MVC5) 中有这 2 个 api:
[HttpGet]
[HttpPost]
public RestErrorHandler Add([FromBody]Models.Customer customer)
{
try
{
//do something...
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
[HttpGet]
[HttpPost]
public RestErrorHandler Delete([FromBody]Models.Customer customer)
{
try
{
//do something
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
我的客户电话是这样的:
var response = client.PostAsync(string.Format("http://my_uri/api/Customer/" + action), contentPost).Result;
其中 'action' 可以是 'Add' 或 'Delete'。
测试删除它告诉我这个错误:
Multiple actions were found that match the request:
为了尝试 'force' 它以正确的方式运行,我在 API:
的方法中添加了 Route 标记 [HttpGet]
[HttpPost]
[Route("Customer/Delete/Customer")]
public RestErrorHandler Delete([FromBody]Models.Customer customer)
{
try
{
//do something
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
但是它调用我的 'Add' api..?
请问我错过了什么?
这两个方法都是POST的,去掉GET。
[RoutePrefix("api")]
public class YourController: ApiController
{
[HttpPost, Route("customers/add")]
public RestErrorHandler Add([FromBody]Models.Customer customer)
{
try
{
//do something...
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
[HttpPost, Route("customers/remove")]
public RestErrorHandler Delete([FromBody]Models.Customer customer)
{
try
{
//do something
return new RestErrorHandler { Error = res.ErrorMessage, Location = MethodBase.GetCurrentMethod().Name };
}
catch (Exception ex)
{
return new RestErrorHandler { Error = ex.ToString(), Location = MethodBase.GetCurrentMethod().Name };
}
}
}
public void Configuration(IAppBuilder app)
{
var configuration = new HttpConfiguration();
//for route attributes on controllers
configuration.MapHttpAttributeRoutes();
}
var response = await client.PostAsync("http://my_uri/api/customers/add", contentPost);
邮递员