6
votes

My WebApi works perfectly on my local machine, but when published to Azure (azure website) I get the following:

No HTTP resource was found that matches the request URI 'http://myazurewebsite.domain/Api/Zipcode/GetLatLong?zip5=23423'.

But on localhost it works lovely.

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

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

I have a modified WebApi Route:

 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 Class:

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);
        }
    }

}
1
Also works fine when deployed as a cloud service, but not as a website.Ray Suelzer
Never figured this out. Started a new project and it worked fine.Ray Suelzer
I thought I'd add a comment as I had the exact same problem today. I finally found a cause/solution: I originally had created a Cloud Service project and then yoinked (technical term) the Web Role out of it to just deploy as a stand alone website (which better suited my needs) however after copying the web role to a separate project there was still one file left over from the original project named 'WebRole.cs' which I had been ignoring. The code in it was preventing from WebApi kicking in. Once I removed it and re-deployed it started working normally...could this have been your problem too?Neil Hibbert
this actually fixed it for me. Thanks a bunch!Mike W

1 Answers

0
votes

Try adding Route as below

    [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);
    }

And then your route config would be as below

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 });


    }
}