locked
"Method Not Allowed" Web API Service Reference RRS feed

  • Question

  • User96650372 posted

    Hello All,

    My team is using Web API for a unique scenario, which was working correctly, until we upgraded the Web API project from OData 3.0 to OData 4.0. We have created a Web API project that, instead of leveraging Microsoft databases, will make web service calls to get the data. However, we are still using the proper technique for retrieving this data, the only difference is that the data pull doesn't get data from databases, but instead makes web service calls. Here are some code examples: 

    This is the ExhibitAController.cs:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web.Http.ModelBinding;
    using System.Web.OData;
    using System.Web.OData.Query;
    using System.Web.OData.Routing;
    using OurProject.Models.ExhibitAModels;
    using OurProject.DataManagers;
    using Microsoft.Data.OData;
    
    namespace OurProject.Controllers
    {
        public class ExhibitAController : ODataController
        {
            private static ODataValidationSettings _validationSettings = new ODataValidationSettings();
    
            // GET: odata/ExhibitA
            public IHttpActionResult GetExhibitA(ODataQueryOptions<ExhibitA> queryOptions)
            {
                // validate the query.
                try
                {
                    queryOptions.Validate(_validationSettings);
                }
                catch (ODataException ex)
                {
                    return BadRequest(ex.Message);
                }
    
                ExhibitADataManager dataManager = new ExhibitADataManager();
                List<ExhibitA> results = dataManager.getExhibitAData("000001");
    
                return Ok<IEnumerable<ExhibitA>>(results);
            }
        }
    }

    This is the WebAPIConfig.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;            
    using System.Web.OData;
    using System.Web.OData.Builder;
    using System.Web.OData.Extensions;
    using System.Web.OData.Routing;
    using OurProject.Models.ExhibitAModels;
    using OurProject.Models;
    
    namespace OurProject
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                    // Call Once
                    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
                 
                    // Exhibit A
                    builder.EntitySet<ExhibitA>("ExhibitA");
    
                    // Call Once
                    ODataRoute route = config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
                   
            }
        }
    }

    The web service calls are not given here, but I can confirm 100% that the web service calls are working fine and bring back data correctly.

    If I run the Web API project in localhost using IIS Express 8.0, and use Postman - Rest Client for Google Chrome, I get results back just fine, complete with a return Status code of 200. I make this call in Postman with: "http://localhost:2530/odata/ExhibitA" 

    However, if I try and do "Add Service Reference" to the Web API project OData endpoint using: "http://localhost:2530/odata/ExhibitA" running in localhost, to another project, in a separate Visual Studio 2013 instance, I get the following error:

    "There was an error downloading 'http://localhost:2530/odata/_vti_bin/ListData.svc/$metadata'.
    The request failed with HTTP status 404: Not Found.
    Metadata contains a reference that cannot be resolved: 'http://localhost:2530/odata'.
    The remote server returned an unexpected response: (405) Method Not Allowed.
    The remote server returned an error: (405) Method Not Allowed.
    If the service is defined in the current solution, try building the solution and adding the service reference again."

    Again, prior to upgrading the project to OData 4.0, using "Add Service Reference" worked perfectly and pulled back all available services within the OData endpoint. Now, I cannot get past this error.

    Any potential solutions to this issue would be very welcome.

    Let me know if you need more information that would assist in solving this problem.

    Thanks in advance,
    Mike

    Thursday, September 4, 2014 3:11 PM

Answers