The first version of ASP.NET Web API was introduced in .NET Framework 4. After that, all the later versions of the .NET Framework support the ASP.NET Web API.
Posted Date:- 2021-10-05 07:47:32
The Internet Media Type is a file identification mechanism on the MIME encoding system. It is also known as the "MIME type." It has become the de facto standard for identifying content on the Internet.
For example, suppose we receive an email from a server with an attachment, then the server embeds the media type of the attachment in the message header. So, the browser can launch the appropriate helper application or plug-in.
Posted Date:- 2021-10-05 07:46:44
The Response Header of each API response consists of an HTTP Status Code. The HTTP Status Codes are the three-digit integers that contain the server response. Here, each number specifies a meaning. The first digit of the Status-Code defines the class of response. According to this first number, the HTTP Status Codes are categorized into five types.
Posted Date:- 2021-10-05 07:45:49
Web API Versioning is a technique in which Web API is arranged to cope with the business changes, and the API will not impact the client that is using/consuming the existing API. As we know, multiple clients can consume the Web API at a time, so Web API versioning is necessary and required as the business grows, and business requirement changes with the time.
Posted Date:- 2021-10-05 07:44:19
In ASP.NET Web API, an HTTP request is used to map to the controller. The Web API framework uses a routing table to determine which action is to invoke.
Posted Date:- 2021-10-05 07:43:39
Yes, we can easily pass multiple complex types in Web API by using the following two methods:
* Using ArrayList
* Using Newtonsoft array
Posted Date:- 2021-10-05 07:42:55
It is placed in the App_Start directory.
App_Start –> WebApiConfig.cs
routes.MapHttpRoute(
name: “myrouteâ€,
routeTemplate: “api/{controller}/{id}â€,
defaults: new { id = RouteParameter.Optional }
);
Posted Date:- 2021-10-05 07:41:50
Parameter Binding is a process that specifies that when a Web API calls a method on a controller, it must set the values for the parameters.
By Default, Web API uses the following rules to bind the parameter:
>> FromUri: If the parameter is of "Simple" type, the Web API tries to get the URI value. Simple Type includes.Net Primitive type like int, double, etc., DateTime, TimeSpan, GUID, string, any type which can convert from the string type.
>> FromBody: If the parameter is of "Complex" type, the Web API only binds the values from the message body.
Posted Date:- 2021-10-05 07:40:43
To enable attribute routing, MapHttpAttributeRoutes(); method can be called in the WebApi config file.
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
// Other Web API configuration not shown.
}
Posted Date:- 2021-10-05 07:39:36
Yes, it can be applied.
[Route(“students/{id:int}â€]
public User GetStudentById(int id) { … }
[Route(“students/{name}â€]
public User GetStudentByName(string name) { … }
You can select the first route whenever the “id†segment of the URI is an integer. Or else, you can choose the second route.
Posted Date:- 2021-10-05 07:38:57
In order to enable SSL to ASP.NET web, you can click on project properties where you can see this option.
Posted Date:- 2021-10-05 07:38:14
public static bool Login(string UN, string pwd)
{
StudentDBEntities students = new StudentDBEntities()
students.sudent.Any(e => e.UserName.Equals(UN) && e=>e.Password.Equlas(UN)) // students has more than one table
}
Posted Date:- 2021-10-05 07:37:26
To make Web API serialize the returning object to JSON format and returns JSON data only. For that you should add the following code in WebApiConfig.cs class in any MVC Web API Project:
//JsonFormatter
//MediaTypeHeaderValue
Config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));
1
2
3
//JsonFormatter
//MediaTypeHeaderValue
Config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"))
Posted Date:- 2021-10-05 07:36:44
WebAPI can be consumed by any client which supports HTTP verbs such as GET, PUT, DELETE, POST. As WebAPI services don’t need any configuration, they are very easy to consume by any client. Infract, even portable devices like Mobile devices can easily consume WebAPI which is certainly the biggest advantages of this technology.
Posted Date:- 2021-10-05 07:36:04
WCF services use the SOAP protocol while HTTP never use SOAP protocol. That’s why WebAPI services are lightweight since SOAP is not used. It also reduces the data which is transferred to resume service. Moreover, it never needs too much configuration. Therefore, the client can interact with the service by using the HTTP verbs.
Posted Date:- 2021-10-05 07:35:31
We can register exception filter globally using following code:
GlobalConfiguration.Configuration.Filters.Add (new MyTestCustomerStore.NotImplExceptionFilterAttribute());
Posted Date:- 2021-10-05 07:34:48
Using Web API tools like Fiddler, we can perform unit testing in Web API. Fiddler is basically a free debugging proxy for any browser that can be used to compose and execute various HTTP requests to Web API and check HTTP response. It is simply used for testing restful web services. It allows one to inspect and check both incoming and outgoing data to monitor and modify requests and responses before the browser receives them. Below is given some setting that is needed to be done fiddler:
Fiddler – Compose Tab -> Enter Request Headers -> Enter Request Body and then execute.
Posted Date:- 2021-10-05 07:34:18
DelegatingHandler is used to develop a custom Server-Side HTTP Message Handler in ASP.NET Web API. It is used to represent Message Handlers before routing in Web API.
Posted Date:- 2021-10-05 07:33:43
CORS (Cross-Origin Resource Sharing) is basically a mechanism that allows one to make requests from one website to another website in a browser that is normally not allowed by another policy called SOP (Same Origin Policy). It supports secure cross-origin requests and data transfers among clients or browsers and servers. Here, cross-origin request means requests coming from different origins. CORS simply resolves the same-origin restriction for JavaScript. One can enable CORS for web API using the respective web API package or OWIN middleware.
Posted Date:- 2021-10-05 07:32:47
Content negotiation is basically a process of selecting the best representation from multiple representations that are available for a given response. It simply allows one to choose rather than negotiate content that one wants to get in response. It is performed at the server-side. In simple words, it chooses the best media type for matters to return a response to an incoming request.
Posted Date:- 2021-10-05 07:32:11
No, we cannot return the view from the ASP.NET Web API method. ASP.NET web API develops HTTP services that provide raw data or information. ApiController in ASP.NET MVC application only renders data that is serialized and sent to the client. One can use a controller to provide normal views.
Posted Date:- 2021-10-05 07:31:23
.NET Framework 4.0 generally supports the first version of ASP.NET Web API. After that, .NET Framework 4.5 supports the latest version of web API i.e., ASP.NET Web API 2.
Posted Date:- 2021-10-05 07:30:34
Exception filter is generally used to handle all unhandled exceptions that are generated in web API. It implements IExceptionFilters interface. It is the easiest and most flexible to implement. This filter is executed whenever the controller method throws any unhandled exception at any stage that is not an HttpResponseExecption exception.
Posted Date:- 2021-10-05 07:29:50
For the action method, there are two ways to map the HTTP request. The first way is using the trait on the action method. The second way is naming the method that starts with the HTTP verb. Taking as an example, to define a GET method, it can be defined as:
1 public void GetEmployee(int id)
2 {
3 StudentRepository.Get(id);
4 }
As it can start with GET, the above-mentioned method can be automatically mapped with the GET request.
Posted Date:- 2021-10-05 07:28:58
Web API is consumed by a client that supports HTTP verbs such as DELETE, GET, PUT, POST. They can quite easily be consumed by a client as Web API services do not need any configuration. Web API can be consumed very easily by portable devices.
Posted Date:- 2021-10-05 07:28:30
Exception filters are used to execute when exceptions are unhandled and thrown from a controller method. There may be several reasons for the exception. Exception filters implement the "IExceptionFilter" interface.
Posted Date:- 2021-10-05 07:28:06
The only protocol supported by Web API is HTTP. Therefore, it can be consumed by a client that supports HTTP protocol.
Posted Date:- 2021-10-05 07:27:40
These are classes that are responsible for response data. The Web API understands the request data format as well as sends data in the format as expected by the clients.
Posted Date:- 2021-10-05 07:27:06
It is not true! It is rather just another way to build non-SOAP based services like plain XML or JSON string. It comes with additional advantages such as using HTTP’s full features and reaching more clients such as mobile devices, etc.
Posted Date:- 2021-10-05 07:26:46
In WEB API HttpError used to throw the error info in the response body. “CreateErrorResponse†method is can also use along with this, which is an extension method defined in “HttpRequestMessageExtension.â€
Posted Date:- 2021-10-05 07:26:25
Code for returning 404 error from HttpError
string message = string.Format(“TestCustomer id = {0} not foundâ€, customerid);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
Posted Date:- 2021-10-05 07:26:03
We can register exception filter from action using following code
[NotImplExceptionFilter]
public TestCust GetMyTestCust (int custno)
{
//write the code
}
Posted Date:- 2021-10-05 07:25:41
MVC is used to create web applications that can return views as well as data; while ASP.NET Web API is used to create restful HTTP services simply, which returns only data and no view. In MVC, the request is mapped to the actions name; while the request is mapped to the actions based on HTTP verbs in Web API.
Posted Date:- 2021-10-05 07:24:59
DelegatingHandler is used in the Web API to represent Message Handlers before routing.
Posted Date:- 2021-10-05 07:24:31
Attribute programming is widely used for this functionality. Web API also allows restricting access of calling methods with the help of specific HTTP verbs. It is also possible to define HTTP verbs as attribute over method.
Posted Date:- 2021-10-05 07:24:09
We can perform a Unit test using Web API tools like Fiddler.
Here, are some setting to be done if you are using
Fiddler –Compose Tab -> Enter Request Headers -> Enter the Request Body and execute
Posted Date:- 2021-10-05 07:23:48
Testing tools for web services for REST APIs include:
1. Jersey API
2. CFX
3. Axis
4. Restlet
Posted Date:- 2021-10-05 07:23:27
Bearer authentication is also known as Token-based authentication.
Posted Date:- 2021-10-05 07:22:56
ASP.Net identity is the membership management framework that Microsoft provides. It is very easily incorporated with Web API. This can help you to build a secure HTTP service.
Posted Date:- 2021-10-05 07:22:30
In ASP.Net Web API, content negotiation is done at the server-side. This helps in determining the media type formatter especially when it is about returning the response to an incoming request.
Posted Date:- 2021-10-05 07:22:05
It is an approach to secure .Net Web API as it authenticates users by a signed token, also called a token-based approach.
Posted Date:- 2021-10-05 07:21:38
Routing is the most important part of ASP.NET Web API. Routing is a way how Web API matches a URI to an action. It is basically a process that decides which action and controller should be called. The controller is basically a class that handles all HTTP requests. All public methods of controllers are basically known as action methods or just actions. Whenever a Web API framework receives any type of request, it routes that request to action.
There are basically two ways to implement routing in Web API as given below:
Convention-based routing: Web API supports convention-based routing. In this type of routing, Web API uses route templates to select which controller and action method to execute.
Attribute-based routing: Web API 2 generally supports a new type of routing known as attribute routing. As the name suggests, it uses attributes to define routes. It is the ability to add routes to the route table via attributes.
Posted Date:- 2021-10-05 07:20:37
ArrayList paramList = new ArrayList();
Category c = new Category { CategoryId = 1, CategoryName =“MobilePhonesâ€};
Product p = new Product { Productcode = 1, Name = “MotoGâ€, Price = 15500, CategoryID = 1 };
paramList.Add(c);
paramList.Add(p);
Posted Date:- 2021-10-05 07:20:17
Two methods to pass the complex types in Web API –
Using ArrayList and Newtonsoft array
Posted Date:- 2021-10-05 07:19:55
With the help of Attributes (like HTTP verbs), It is possible to implement access restrictions in Web API.
It is possible to define HTTP verbs as an attribute to restrict access.
Example:
[HttpPost]
public void Method1(Class obj)
{
//logic
Posted Date:- 2021-10-05 07:19:39
Basic Authentication in ASP.Net Web API can be implemented where the client sends a request with an Authorization header and word Basic. In Basic Authentication, the Authorization header contains a word Basic followed by a base 64 encoded string.
The syntax for Basic Authentication –
Authorization: Basic username: password
Posted Date:- 2021-10-05 07:19:01
It supports the following return types:
HttpResponseMessage
IHttpActionResult
Void
Other types such as string, int, etc.
Posted Date:- 2021-10-05 07:18:32
WCF: It is a framework used for developing SOAP (Service Oriented Applications Protocols). It also supports various transport protocols as given above.
ASP.NET Web API: It is a framework used for developing non-SOAP-based services. It is limited to HTTP-based services.
No, it's not true that ASP.NET Web API has replaced WCF. WCF was generally developed to develop SOAP-based services. ASP.NET Web API is a new way to develop non-SOAP-based services such as XML, JSON, etc. WCF is still considered a better choice if one has their service using HTTP as the transport and they want to move to some other transport like TCP, NetTCP, MSMQ, etc. WCF also allows one-way communication or duplex communication.
Posted Date:- 2021-10-05 07:18:03
ASP.NET Web API includes a number of new exciting features as given below:
* Attribute Routing
* CORS (Cross-Origin Resource Sharing)
* OWIN (Open Web Interface for .NET) self-hosting
* IHttpActionResult
* Web API OData
Posted Date:- 2021-10-05 07:17:26
It is possible to register exception filter globally using following code-
GlobalConfiguration.Configuration.Filters.Add(new
MyTestCustomerStore.NotImplExceptionFilterAttribute());
Posted Date:- 2021-10-05 07:16:42