我正在尝试对本地控制器进行API调用,然后对远程服务器进行API调用。我能够点击本地,但是当我调用远程服务器时,我收到404错误作为响应。我有其他方法在第二个服务器的控制器中非常相似,工作得很好。我不知道为什么......
遥控器方法:
// POST: api/MLSData
[ResponseType(typeof(HttpResponseMessage))]
[Route("MLSReport/DeleteMLSData/")]
[HttpPost]
public HttpResponseMessage DeleteMLSData(int id)
{
MLSData MLSFromDB = GenerateMLSReport.DeleteMLSData(id, db);
if (MLSFromDB != null)
{
return Request.CreateResponse(HttpStatusCode.Accepted);
}
else
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
}
本地控制器方法:
[ResponseType(typeof(HttpStatusCode))]
[Route("{lang}/api/MLSReport/MLSDelete/{id}")]
[HttpGet]
public async Task<HttpStatusCode> MLSDelete(int id)
{
//start API call
string baseProcessingURL = WebConfigurationManager.AppSettings["ProcessingDatabaseURL"];
string path = " MLSReport/DeleteMLSData/";
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(baseProcessingURL);
HttpResponseMessage result = await client.PostAsJsonAsync(path, id);
return result.StatusCode;
}
}//end MLSDelete
每个请求的路由代码:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configuring CORS - http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api //
var cors = new EnableCorsAttribute("*", "Origin, Content-Type, Accept", "GET, POST, PUT, DELETE, OPTIONS");
config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}