2013-10-12 100 views
3

我的WebAPI完美的作品我的本地机器上,但在发布到Azure的(天青网站)我得到以下几点:的WebAPI不工作当部署到Azure的网站

没有HTTP资源发现,请求URI匹配 'http://myazurewebsite.domain/Api/Zipcode/GetLatLong?zip5=23423'。

但在本地主机上,它的工作可爱。

http://localhost/Api/Zipcode/GetLatLong?zip5=20024 

{"Latitude":38.89,"Longitude":-77.03} 

我有一个改进的WebAPI路线:

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     config.Routes.MapHttpRoute(
      name: "DefaultPublicApi", 
      routeTemplate: "Api/{controller}/{action}/{id}/{format}", 
      defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional} 
     ); 
    } 
} 

ApiController类:

using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using Project.Geography.Services; 
using Project.WebPublic.Filters; 

namespace Project.WebPublic.Controllers.WebApi 
{ 
    public class ZipCodeController : ApiController 
    { 
     private readonly ZipCodeService _zipCodeService; 

     public ZipCodeController(ZipCodeService zipCodeService) 
     { 
      _zipCodeService = zipCodeService; 
     } 


     [HttpGet] 
     [TransactionFilter] 
     public HttpResponseMessage GetLatLong(string zip5) 
     { 
      if (zip5.Length != 5) 
       return Request.CreateResponse(HttpStatusCode.BadRequest, "Zip Code Length Not Equal to 5"); 

      var zip = _zipCodeService.GetByZip5(zip5); 

      if (zip == null) 
       return Request.CreateResponse(HttpStatusCode.NotFound, "Could not find Zip Code in Database"); 

      var latlong = new 
      { 
       Latitude = zip.Latitude, 
       Longitude = zip.Longitude 
      }; 

      return Request.CreateResponse(HttpStatusCode.OK, latlong); 
     } 
    } 

} 
+0

当部署为云服务时也可以正常工作,但不能用作网站。 –

+0

从来没有想通了。开始一个新项目,它运行良好。 –

+3

我想我会添加评论,因为我今天有完全相同的问题。我终于找到了一个原因/解决方案:我原本创建了一个Cloud Service项目,然后将Web角色(技术术语)从其中部署到仅作为独立网站进行部署(这更适合我的需求),但复制Web角色后到一个单独的项目中,还有一个我原来忽略的名为'WebRole.cs'的项目遗留下来的文件。其中的代码阻止了WebApi的踢入。一旦我删除它并重新部署它开始正常工作......这可能是你的问题吗? –

回答

0

尝试增加路线如下

[HttpGet] 
    [TransactionFilter] 
    [Route("api/GetLatLong")] 
    public HttpResponseMessage GetLatLong(string zip5) 
    { 
     if (zip5.Length != 5) 
      return Request.CreateResponse(HttpStatusCode.BadRequest, "Zip Code Length Not Equal to 5"); 

     var zip = _zipCodeService.GetByZip5(zip5); 

     if (zip == null) 
      return Request.CreateResponse(HttpStatusCode.NotFound, "Could not find Zip Code in Database"); 

     var latlong = new 
     { 
      Latitude = zip.Latitude, 
      Longitude = zip.Longitude 
     }; 

     return Request.CreateResponse(HttpStatusCode.OK, latlong); 
    } 

然后你的路线的配置将是如下

public static class WebApiConfig 
{ 
    /// <summary> 
    /// Register the http configuration for web API. 
    /// </summary> 
    /// <param name="config">The http configuration instances</param> 
    public static void Register(HttpConfiguration config) 
    { 
     config.MapHttpAttributeRoutes(); 
     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional }); 


    } 
}