In this I am going to explain
how to create First Web API Hello World application with repository pattern.
I am using Visual studio
2019 and .net framework to create this application.
Implementation:
STEP 1 :
Open VS 2019. Click on create a new project and select ASP.Net web application
(.Net Framework).
STEP 2 : Configure
new project window will be open. Enter the project Name, select location and
click on Create button.
STEP 3 :
Create a new ASP.NET Web application window will be open. Select Empty and
check the Web API from add folders & core reference. If you want to create MVC
+ Web API, then select the MVC option.
Application will be
created and looks as below attached screenshot.
Firstly, you need to check
the important files of Web API application WebApiConfig.cs
and Global.asax
WebApiConfig.cs
This file is very
important because it configure the routes of incoming request. This file is
located under App_Start folder and
looks as shown below:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web
API configuration and services
// Web
API routes
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Global.asax
This file has Application_Start
method. This method is executed when application starts for first time. As you
see Web API routes are configured in this method. If you check the definition
of this, will redirect you to WebApiConfig.cs file.
protected void
Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
STEP 4: Add an interface to project
Firstly, we need to create a folder for interface. Right click
on project name >> Select add >> select the new folder. Rename the folder name. I have named it as
Interface.
Now right click on interface folder and select class. Select
the interface from given options. I have named it as IHello.cs
Complete
code of Interface (Ihello.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WEBAPI.Models;
namespace WEBAPI.Interface
{
public interface IHello
{
string HelloMethod();
}
}
STEP 5:
Add service to project
Similarly, to interface add a folder to project and named its
as Service. Right click on Service folder and select class. Select the class from given options.
Complete
code of Service (HelloService.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WEBAPI.Interface;
using WEBAPI.Models;
namespace WEBAPI.Services
{
public class HelloService : IHello
{
public string
HelloMethod()
{
return "First Web API Hello World
application with repository pattern";
}
}
}
STEP 6 : Add
controller
Now let move to next step. Add controller to project. Right
click on Controllers folder >> Add >> select Controller. Select Web
API 2 Controller-Empty, click on add button and enter controller name.
STEP 7: Add Method
Create a method. I have created a method (HelloMethod) and
return the first application text.
Complete
code of Controller (HelloController.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WEBAPI.Models;
using WEBAPI.Interface;
namespace WEBAPI.Controllers
{
public class HelloController : ApiController
{
private IHello _hello;
public HelloController(IHello hello)
{
_hello = hello;
}
[HttpGet]
public string HelloApplication
()
{
return _hello.HelloMethod();
}
}
}
STEP 8 : Enable
dependency injection using Unity
To enable the dependency injection in this project I am using Unity Container. Install the Unity
Webapi using NuGet package.
install-package unity.webapi
This will create a class (UnityConfig.cs)
in App_Start folder. Open this file and map the interface and class dependencies.
Means need to register the created service class to Interface.
container.RegisterType<IHello, HelloService>();
Complete
code of UnityConfig.cs
using System.Web.Http;
using Unity;
using Unity.WebApi;
using WEBAPI.Interface;
using WEBAPI.Services;
namespace WEBAPI
{
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
//
register all your components with the container here
// it is
NOT necessary to register your controllers
// e.g.
container.RegisterType<ITestService, TestService>();
container.RegisterType<IHello,
HelloService>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
}
STEP 9: Now we
need to add the Unity Registercomponents in Application_Start method of Global.asax file.
Complete
code of Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing;
namespace WEBAPI
{
public class WebApiApplication :
System.Web.HttpApplication
{
protected void
Application_Start()
{
UnityConfig.RegisterComponents();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}
All done, now run the application. Type the URL according to
WebApiConfig.cs file like Api/controller/Action/id e.g. https://localhost:44373/api/hello
No comments:
Post a Comment