locked
unable to get webapi result with parameter RRS feed

  • Question

  • User751840860 posted

    am very new to webapi--

    i went to this article https://www.c-sharpcorner.com/article/create-simple-web-api-in-asp-net-mvc/ and tried to create the web api with VS 2017, and am getting the correct results

    when i typed, http://localhost:8787/api/Home/get 

    it returns the string .

    but when i passed the same http://localhost:8787/api/Home/5  

    with paramater , it didnt give me the accurate results.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Http;
    using System.Web.Mvc;

    namespace DemoWebAppi2.Controllers
    {
    public class HomeController : ApiController
    {
    public string Get()
    {
    return "Welcome to web api";
    }

    public List<string> GetWithID(int mid)
    {
    return new List<string> { "string1", "string2" };
    }
    }
    }

    above is my code:

    routeconfig file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    namespace DemoWebAppi2
    {
    public class RouteConfig
    {
    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    }
    }
    }

    pls help, how to pass the url and get the accurate results ? i have not changed anything in the routeconfig file. i created this using  VS 2017 

    Thursday, April 25, 2019 12:50 PM

Answers

All replies

  • User475983607 posted

    Unclear without the code, steps to reproduce, expected results, and actual results.

    Edit: I see you added the code.  You actions name is incorrect and the input parameter name is most likely incorrect.  you did not post the route config so can't be sure.

    This works which is the default API template in VS.

        public partial class ValuesController : ApiController
        {
            [HttpGet]
            public string Get()
            {
                return "Hello World";
            }
    
            // GET api/values/5
            [HttpGet]
            public int Get(int id)
            {
                return id;
            }

    Thursday, April 25, 2019 1:05 PM
  • User751840860 posted

    so, in order to pass the parameter, should i always use get() ?

    cant i use some other method names?

    ps: am sorry, if this is a stupid question

    Thursday, April 25, 2019 2:01 PM
  • User475983607 posted

    so, in order to pass the parameter, should i always use get() ?

    cant i use some other method names?

    No, all the action types have the ability to accept input parameters.

    I recommend that you go through a few tutorials to learn the fundamentals.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 25, 2019 2:05 PM
  • User751840860 posted

    thanks, am not getting the actual results even after adding [System.Web.Http.HttpGet] attribute.

    Thursday, April 25, 2019 2:08 PM
  • User475983607 posted

    SaMolPP

    thanks, am not getting the actual results even after adding [System.Web.Http.HttpGet] attribute.

    I cannot see your updated code.  Did you change the action name to "Get" and change the input name to "Id" too?

    Otherwise the URL is...

    http://localhost:52803/api/home/?mid=5

    Thursday, April 25, 2019 2:15 PM
  • User751840860 posted

    thanks mgebhard!

    it worked now.

    Monday, April 29, 2019 11:12 AM