locked
A Weird Case of Routing Where One Method in the Same Controller is Callable while the Another gives a 404 Error RRS feed

  • Question

  • User-1582807108 posted

    Hello All ... I have something here that is really puzzling me and I am hoping someone could point me in
    the right direction ..

    Why would one method be calleable and another one not  calleable when their structure are almost basically
    identical and housed in the same class .. details below in comments on  lines # 1 and 13 :

    What is most puzzling is that despite (in the code below) the fact that both GetCustomerRecordById  and GetBoatsByCustomerID 
    are inside the same class CustomerController ( I was actually hoping I could put the Boat call in a BoatController
    but having failed  there with a 404 error I decided to house it in class CustomerController only to end up with
    the exact  same error) the first one is successfully hit by  Fiddler  and and other is not ... I verified the results of
    Fiddler by running the addresses straight from the browser and as expected I am getting the same error
    when trying out the http://localhost:52581/GetBoatsByCustomerID/1  described below so its really weird  .. any
    advise appreciated .. thanks again

    0 public class CustomerController : ApiController
      {
    .
    .
    .
    1 [HttpGet] // works okay ... Fiddler is able to hit the line# 5  below and use  http://localhost:52581/GetCustomerRecordById/1									
    2 [Route("GetCustomerRecordById/{id}")]   								
    3 public IList<MarinaData.DataLayer.Customer> GetCustomerRecordById(int id)									
    4 {									
    5    MarinaEntities mentities = new MarinaEntities();									
    6    var customerData = from c in mentities.Customers									
    7                       where c.ID == id									
    8                       select c;									
    9    List<MarinaData.DataLayer.Customer> customers = new List<MarinaData.DataLayer.Customer>();									
    10    customers = customerData.ToList<MarinaData.DataLayer.Customer>();									
    11    return customers;									
    12 }									
    13 [HttpGet]  //getting error private .. Fiddler is not able to hit the line number 17 below and instead returns an  HTTP/1.1 404 Not Found error when running address http://localhost:52581/GetBoatsByCustomerID/1		
    14 [Route("GetBoatsByCustomerID/{id}")]									
    15 public static List<MarinaData.DataLayer.Boat> GetBoatsByCustomerID(int id)									
    16 {									
    17    MarinaEntities mentities = new MarinaEntities();									
    18    var boatData = from c in mentities.Boats									
    19                   where c.ID == id									
    20                   select c;									
    21    List<MarinaData.DataLayer.Boat> boats = new List<MarinaData.DataLayer.Boat>();									
    22    boats = boatData.ToList<MarinaData.DataLayer.Boat>();									
    23    return boats;	
    .
    .
    .
    }								
    24 }

    Code below is just additional info showing the content of WebApiConfig.cs

        public static class WebApiConfig
        {
    
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
                var json = config.Formatters.JsonFormatter;
                json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
                config.Formatters.Remove(config.Formatters.XmlFormatter);
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }


    Friday, June 19, 2015 12:53 AM

Answers

  • User-1582807108 posted

    Thanks ... I found the solution to the problem and it is just the text static that I removed and now everything works perfect .. please consider this question closed and solved ... thanks again

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 19, 2015 1:13 AM

All replies

  • User-1582807108 posted

    Thanks ... I found the solution to the problem and it is just the text static that I removed and now everything works perfect .. please consider this question closed and solved ... thanks again

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 19, 2015 1:13 AM
  • User438962230 posted

    Thank you for comming back and close this case. Welcome to post here again.

    Tuesday, June 23, 2015 4:57 AM