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}");
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;
}
Member
14 Points
83 Posts
unable to get webapi result with parameter
Apr 25, 2019 12:50 PM|SaMolPP|LINK
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
All-Star
53751 Points
24069 Posts
Re: unable to get webapi result with parameter
Apr 25, 2019 01:05 PM|mgebhard|LINK
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.
Member
14 Points
83 Posts
Re: unable to get webapi result with parameter
Apr 25, 2019 02:01 PM|SaMolPP|LINK
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
All-Star
53751 Points
24069 Posts
Re: unable to get webapi result with parameter
Apr 25, 2019 02:05 PM|mgebhard|LINK
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
Member
14 Points
83 Posts
Re: unable to get webapi result with parameter
Apr 25, 2019 02:08 PM|SaMolPP|LINK
thanks, am not getting the actual results even after adding [System.Web.Http.HttpGet] attribute.
All-Star
53751 Points
24069 Posts
Re: unable to get webapi result with parameter
Apr 25, 2019 02:15 PM|mgebhard|LINK
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...
Member
14 Points
83 Posts
Re: unable to get webapi result with parameter
Apr 29, 2019 11:12 AM|SaMolPP|LINK
thanks mgebhard!
it worked now.