locked
Default routing is not working any help on this. RRS feed

  • Question

  • User604186779 posted

    Hi I have created products controller which has some methods called

    [Route("GetAllProducts")]
    public IEnumerable<Product> GetAllProducts()
    {
    return products;
    }
    
    [Route("GetProduct/{id}")]
    public IHttpActionResult GetProduct(int id)
    {
    var product = products.FirstOrDefault((p) => p.Id == id);
    if (product == null)
    {
    return NotFound();
    }
    return Ok(product);
    }

    So when i browser the below url it shows the error "the resource cannot be found"

    http://localhost:59315/api/products/GetAllProducts

    but the action method which has parameter which was working fine.

    http://localhost:59315/api/products/GetProduct/2

    my webapiconfig.cs is look like below.

      config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

    Please let me know if you have any inputs.

    Friday, March 25, 2016 7:02 AM

Answers

  • User604186779 posted

    Previously  I was working with VS 2012 there i am facing the above issue. After that I have installed VS 2013 and tried the same its working fine.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 29, 2016 6:20 AM

All replies

  • User36583972 posted

    Hi venkatu2005,

    According to your webapiconfig file, you should change your methods like the below.

     // http://localhost:29679/api/country/GetAllProducts
            [HttpGet]
            [ActionName("GetAllProducts")]
            public IEnumerable<string> GetAllProducts()
            {
                return new string[] { "value1", "value2" };
            }
    
            //http://localhost:29679/api/country/GetProduct/1
            [HttpGet]
            [ActionName("GetProduct")]
            public IHttpActionResult GetProduct(int id)
            {
                if (id == 0)
                {
                    return NotFound();
                }
                return Ok("value = " + id.ToString());
            }

    Routing in ASP.NET Web API:

    http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    Best Regards,

    Yohann Lu

    Friday, March 25, 2016 8:24 AM
  • User604186779 posted

    Hi Lu,

    Still I am facing the same issue. "resource cannot be found'

    Friday, March 25, 2016 8:33 AM
  • User36583972 posted

    Hi venkatu2005,

    If your API project is individually created. The methods I provided above is working. Or you created a asp.net mvc app and added default web api controller?

    You can go check the following points:

    •No type with {controller} name has been found.

    •No matching action method found in the selected controller due to no action method start with the request HTTP method verb or no action method with IActionHttpMethodProviderRoute implemented attribute found or no method with {action} name found or no method with the matching {action} name found.

    You can refer the following tutorial for getting more information:

    https://dzone.com/articles/handling-http-404-error-aspnet

    I suggest you can share us more relevant code or information to help us solve your problem.

    Best Regards,

    Yohann Lu

    Friday, March 25, 2016 9:35 AM
  • User604186779 posted

    Previously  I was working with VS 2012 there i am facing the above issue. After that I have installed VS 2013 and tried the same its working fine.

    Thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 29, 2016 6:20 AM