Monday, 11 July 2016

How to Host ASP.Net Web API on IIS Server

http://www.c-sharpcorner.com/UploadFile/2b481f/how-to-host-Asp-Net-web-api-on-iis-server/


Internet Information Services (IIS)
IIS is the acronym for Internet Information Services. It is the set of feature extension modules that are created by Microsoft. IIS is an integral part of the Windows Server products. It supports the Hyper Text Transfer Protocol (HTTP), File Transfer Protocol (FTP), Simple Mail Transfer Protocol (SMTP) etc. IIS is not turned on automatically when Windows is installed.
Benefits of IIS Hosting
  • When we host the Web API in IIS , it is deployed and managed like other IIS applications.
  • When we host the application on IIS, it can provide the process activation and recycling capabilities that increase the reliability of the hosted application.
  • The Web API is hosted on IIS, that simplifies the development and deployment of hosted services.
Now I will define the procedure of hosting the Web API on an IIS server.
Step 1
First open IIS. We can start it from the Start menu; enter "inetmgr" into the search box. The image looks like this:
 iis.jpg
We can open it using the Control Panel.
  • In the "Control Panel".
  • Click on "Administrative Tools" -> "Internet Information Services (IIS) manager".
  • The image is displayed in the following picture.
iis1.jpg
If you are not able to see the IIS manager then your system might not have IIS installed. For the installation follow this procedure:
  • Open the "Control Panel".
  • "Programs and Feature" -> "Turn Window features on or off".
iis2.jpg
  • There is open the "Windows Feature" window in "tree view".
  • There are checkboxes for installation; select the checkboxes.
iis3.jpg
Step 2
When you will open the Internet Information Services (IIS) manger, the window looks like this:
iis16.jpg
  • Now, right-click on the sites.
  • "Sites" -> "Add Web Site".
iis4.jpg
Open a window. It looks likes this:
iis5.jpg
In this window write the "Site name" here the site name is "Testing" and  provide the Physical path.
When we click on the "Ok" button, a alert box is opened. It display the information about the port; the default port is "80" for the IIS website and if you want to change it then click on the "NO" button.
iis6.jpg
And change the port. Here I will provide the port "8080". And now click on the "OK" button.
iis7.jpg
Now a window is opened that looks like this:
iis8.jpg
When we click on the Testing site from the Sites, then it opens as:
iis9.jpg
Step 3
Now we check the "Advanced setting" from the "right panel". And modify some settings.
iis10.jpg

Step 4
Now edit the "Basic settings" in the "Edit Application Pool" in the right panel. In it we select the ".NET Framework version" that is ".NET Framework v4.0.30319" and "Managed pipeline mode" as "Integrated".
 iis12.jpg
Step 5
In this step we are ready to browse our application.
  • Right-click on your website.
  • Select "Manage Web Site" -> "Browse".
iis11.jpg
  • The application is hosted. There is my application "Testing" Host at "8080" port that looks like this:
iis15.jpg
 

Wednesday, 6 July 2016

Web API Interview Questions


1) What is Web API?

It is a framework which helps us to build/develop HTTP services. So there will a client server communication using HTTP protocol.

2) What is Representational state transfer or REST?

REST is architectural style, which has defined guidelines for creating services which are scalable. REST used with HTTP protocol using its verbs GET, POST, PUT and DELETE.

3) Explain Web API Routing?

Routing is the mechanism of pattern matching as we have in MVC. These routes will get registered in Route Tables. Below is the sample route in Web API –

Routes.MapHttpRoute(
 Name: "MyFirstWebAPIRoute",
 routeTemplate: “api/{controller}/{id}
 defaults: new { id = RouteParameter.Optional}
};

4) List out the differences between WCF and Web API?

      WCF

  • It is framework build for building or developing service oriented applications. 
  • WCF can be consumed by clients which can understand XML. 
  • WCF supports protocols like – HTTP, TCP, Named Pipes etc.
    Web API

  • It is a framework which helps us to build/develop HTTP services
  • Web API is an open source platform.
  • It supports most of the MVC features which keep Web API over WCF.
    5) What are the advantages of using REST in Web API?
    REST always used to make less data transfers between client and server which makes REST an ideal for using it in mobile apps. Web API supports HTTP protocol thereby it reintroduces the old way of HTTP verbs for communication.
    6) Difference between WCF Rest and Web API?
    WCF Rest

  • WebHttpBinding” to be enabled for WCF Rest.
  • For each method there has to be attributes like – “WebGet” and “WebInvoke
  • For GET and POST verbs respectively.
    Web API

  • Unlike WCF Rest we can use full features of HTTP in Web API.
  • Web API can be hosted in IIS or in application.
    7) List out differences between MVC and Web API?
    Below are some of the differences between MVC and Web API 
    MVC

  • MVC is used to create a web app, in which we can build web pages.
  • For JSON it will return JSONResult from action method.
  • All requests are mapped to the respective action methods.
    Web API

  • This is used to create a service using HTTP verbs.
  • This returns XML or JSON to client.
  • All requests are mapped to actions using HTTP verbs.
    8) What are the advantages of Web API?
    Below are the list of support given by Web API –

  • OData
  • Filters
  • Content Negotiation
  • Self Hosting
  • Routing
  • Model Bindings
    9) Can we unit test Web API?
    Yes we can unit test Web API.
    10) How to unit test Web API?
    We can unit test the Web API using Fiddler tool. Below are the settings to be done in Fiddler –
    Compose Tab -> Enter Request Headers -> Enter the Request Body and execute
    11) Can we return view from Web API?
    No. We cannot return view from Web API.
    12) How we can restrict access to methods with specific HTTP verbs in Web API?
    Attribute programming is used for this functionality. Web API will support to restrict access of calling methods with specific HTTP verbs. We can define HTTP verbs as attribute over method as shown below

[HttpPost]
 public void UpdateTestCustomer(Customer c)
 {
 TestCustomerRepository.AddCustomer(c);
 }

13) Can we use Web API with ASP.NET Web Forms?

Yes. We can use Web API with ASP.NET Webforms.

14) List out the steps to be made for Web API to work in Web Forms?

Below are the steps to be followed –

  • Creating new controller for Web API.
  • Adding routing table to “Application_Start” method in Global.asax
  • Make a AJAX call to Web API actions.
    15) Explain how to give alias name for action methods in Web API?
    Using attribute “ActionName” we can give alias name for Web API actions. Eg:

[HttpPost]
[ActionName("AliasTestAction")]
 public void UpdateTestCustomer(Customer c)
 {
 TestCustomerRepository.AddCustomer(c);
 }

 

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API?

ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework.
Using ASP.NET Web API we can create non-SOAP based services like plain XML or JSON strings etc. with many other advantages including:
  • Create resource-oriented services using the full features of HTTP.
  • Exposing services to a variety of clients easily like browsers or mobile devices etc.

What are the advantages of using ASP.NET Web API?

Using ASP.NET Web API has a number of advantages, but core of the advantages are:

  • It works the HTTP way using standard HTTP verbs like GET, POST, PUT, DELETE etc for all CRUD operations.
  • Complete support for routing.
  • Response generated in JSON or XML format using MediaTypeFormatter.
  • It has the ability to be hosted in IIS as well as self-host outside of IIS.
  • Supports Model binding and Validation.
  • Support for OData.
  • and more....
For implementation on performing all CRUD operations using ASP.NET Web API.

What new features are introduced in ASP.NET Web API 2.0?

More new features introduced in ASP.NET Web API framework v2.0 are as follows:
  • Attribute Routing
  • External Authentication
  • CORS (Cross-Origin Resource Sharing)
  • OWIN (Open Web Interface for .NET) Self Hosting
  • IHttpActionResult
  • Web API OData

WCF Vs ASP.NET Web API?

Actually, Windows Communication Foundation is designed to exchange standard SOAP-based messages using variety of transport protocols like HTTP, TCP, NamedPipes or MSMQ etc.
On the other hand, ASP.NET API is a framework for building non-SOAP based services over HTTP only.

Is it true that ASP.NET Web API has replaced WCF?

It's a misconception that ASP.NET Web API has replaced WCF. It's another way of building non-SOAP based services, for example, plain XML or JSON string etc.
Yes, it has some added advantages like utilizing full features of HTTP and reaching more clients such as mobile devices etc.
But WCF is still a good choice for following scenarios:
  • If we intended to use transport other than HTTP e.g. TCP, UDP or Named Pipes.
  • Messag Queuing scenario using MSMQ.
  • One-way communication or Duplex communication

MVC Vs ASP.NET Web API?

As in previous ASP.NET Web API Interview Questions, we discussed that purpose of Web API framework is to generate HTTP services that reaches more clients by generating data in raw format, for example, plain XML or JSON string. So, ASP.NET Web API creates simple HTTP services that renders raw data.
On the other hand, ASP.NET MVC framework is used to develop web applications that generates Views as well as data. ASP.NET MVC facilitates in rendering HTML easy.
MVC Vs Web API

How to return View from ASP.NET Web API method?

(A tricky Interview Question) No, we can't return view from ASP.NET Web API Method. As we discussed in earlier interview question about difference between ASP.NET MVC and Web API that ASP.NET Web API creates HTTP services that renders raw data. Although, it's quite possible in ASP.NET MVC application.

How to restrict access to Web API method to specific HTTP Verb?

Attribute programming plays it's role here. We can easily restrict access to an ASP.NET Web API method to be called using a specific HTTP method. For example, we may required in a scenario to restrict access to a Web API method through HTTP POST only as follows:
 [HttpPost]
 public void UpdateStudent(Student aStudent)
 {
       StudentRepository.AddStudent(aStudent); 
 }

Can we use Web API with ASP.NET Web Form?

Yes, ASP.NET Web API is bundled with ASP.NET MVC framework but still it can be used with ASP.NET Web Form.
It can be done in three simple steps as follows:
  1. Create a Web API Controller.
  2. Add a routing table to Application_Start method of Global.asax.
  3. Make a jQuery AJAX Call to Web API method and get data.

How we can provide an alias name for ASP.NET Web API action?

We can provide an alias name for ASP.NET Web API action same as in case of ASP.NET MVC by using "ActionName" attribute as follows:
 [HttpPost]
[ActionName("SaveStudentInfo")]
 public void UpdateStudent(Student aStudent)
 {
       StudentRepository.AddStudent(aStudent); 
 }

What is Web API and why to use it ?

ASP.Net Web API is a framework for building HTTP services that can be consumed by a broad range of clients including browsers, mobiles, iphone and tablets. It is very similar to ASP.NET MVC since it contains the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection. But it is not a part of the MVC Framework. It is a part of the core ASP.NET platform and can be used with MVC and other types of Web applications like Asp.Net WebForms. It can also be used as an stand-alone Web services application.

Why Asp.Net Web API (Web API) ?

Today, a web-based application is not enough to reach it's customers. People are very smart, they are using iphone, mobile, tablets etc. devices in its daily life. These devices also have a lot of apps for making the life easy. Actually, we are moving from the web towards apps world.
So, if you like to expose your service data to the browsers and as well as all these modern devices apps in fast and simple way, you should have an API which is compatible with browsers and all these devices.
For example twitter,facebook and Google API for the web application and phone apps.
Web API is the great framework for exposing your data and service to different-different devices. Moreover Web API is open source an ideal platform for building REST-ful services over the .NET Framework. Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats) and you don't need to define any extra config settings for different devices unlike WCF Rest service.

Web API Features

  1. It supports convention-based CRUD Actions since it works with HTTP verbs GET,POST,PUT and DELETE.
  2. Responses have an Accept header and HTTP status code.
  3. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.
  4. It may accepts and generates the content which may not be object oriented like images, PDF files etc.
  5. It has automatic support for OData. Hence by placing the new [Queryable] attribute on a controller method that returns IQueryable, clients can use the method for OData query composition.
  6. It can be hosted with in the applicaion or on IIS.
  7. It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection that makes it more simple and robust.

Why to choose Web API ?

  1. If we need a Web Service and don’t need SOAP, then ASP.Net Web API is best choice.
  2. It is Used to build simple, non-SOAP-based HTTP Services on top of existing WCF message pipeline.
  3. It doesn't have tedious and extensive configuration like WCF REST service.
  4. Simple service creation with Web API. With WCF REST Services, service creation is difficult.
  5. It is only based on HTTP and easy to define, expose and consume in a REST-ful way.
  6. It is light weight architecture and good for devices which have limited bandwidth like smart phones.
  7. It is open source.