>First you have to add three files in project 1)Global.asax 2) testApiController.cs by adding web api controller class in project(add new item) 3)WebApiConfig.cs file
> Add below code in these files
Global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
YashWebsiteWebForm.WebApiConfig.Register(System.Web.Http.GlobalConfiguration.Configuration);
}
WebApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
testApiController.cs
public class testApiController : ApiController
{
// GET api/values
//public IEnumerable<string> Get()
//{
// return new string[] { "value1", "value2" };
//}
public string Get()
{
return "Welcome To Web API";
}
and run the project with localhost:0000/api/testapi
Note if you give file name of controller with abccontroller1.cs than it will give error
if you want to get the value by id than use localhost:0000/api/testapi/1
Also there are different classes for creating web api eg { (Controller, Controllerbase) These are from System.Web.Mvc namespace}, ApiController. I am using APIController
routeTemplate: "api/{controller}/{id}", // This can be change to "webapi/{controller}/{id}" etc.
No comments:
Post a Comment