Found 2628 Articles for Csharp

What is Content Negotiation in Asp.Net webAPI C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:23:03

2K+ Views

Content negotiation is the process of selecting the best representation for a given response when there are multiple representations available. Means, depending on the Accept header value in the request, the server sends the response. The primary mechanism for content negotiation in HTTP are these request headers −Accept − Which media types are acceptable for the response, such as "application/json, " "application/xml, " or a custom media type such as "application/vnd.example+xml"Accept-Charset − Which character sets are acceptable, such as UTF-8 or ISO 8859-1.Accept-Encoding − Which content encodings are acceptable, such as gzip.Accept-Language − The preferred natural language, such as "en-us".The ... Read More

What is the use of ChildActionOnly attribute in ASP .Net MVC C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:20:26

864 Views

Child Action is only accessible by a child request. It will not respond to the URL requests. If an attempt is made, a runtime error will be thrown stating - Child action is accessible only by a child request. Child action methods can be invoked by making child request from a view using Action() and RenderAction() html helpers.Child action methods are different from NonAction methods, in that NonAction methods cannot be invoked using Action() or RenderAction() helpers.Below is the Child Action Error when we try to invoke it using URL.ControllerExampleusing System.Collections.Generic; using System.Web.Mvc; namespace DemoMvcApplication.Controllers{    public class HomeController : ... Read More

What are the levels at which filters can be applied in ASP .Net MVC C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:17:38

383 Views

In an ASP .Net MVC application filters can be applied in three levels.Action Method LevelController LevelGlobal LevelAction Method LevelFilters that are applied at the Action Method level will work only particularly for that action method.using System.Web.Mvc; namespace DemoMvcApplication.Controllers{    public class HomeController : Controller{       [Authorize] //Action Method Level       public string Index(){          return "Index Invoked";       }    } }Controller LevelController level filters are applied to all the action methods. The following filter are applicable to all the action methods of the HomeController, but not on other controllers.using System.Web.Mvc; ... Read More

What are the three segments of the default route, that is present in ASP .Net MVCC#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:15:18

795 Views

The ASP.Net MVC Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match.ASP.NET introduced Routing to eliminate the needs of mapping each URL with a physical file. Routing enables us to define ... Read More

What is the significance of NonActionAttribute in ASP .Net MVC C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:10:35

3K+ Views

The NonAction attribute is used when we want a public method in a controller but do not want to treat it as an action method. An action method is a public method in a controller that can be invoked using a URL. So, by default, if we have any public method in a controller then it can be invoked using a URL request. To restrict access to public methods in a controller, NonAction attribute can be used.Now let us consider HomeController having two public methods MyMethod1 and MyMethod2.ControllerExampleusing System.Web.Mvc; namespace DemoMvcApplication.Controllers{    public class HomeController : Controller{       ... Read More

How to use ViewBag in ASP .Net MVC C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:07:44

3K+ Views

ViewBag uses the dynamic feature that was introduced in to C# 4.0. It allows an object to have properties dynamically added to it. Internally, it is a dynamic type property of the ControllerBase class which is the base class of the Controller class.ViewBag only transfers data from controller to view, not visa-versa. ViewBag values will be null if redirection occurs. ViewBag is able to set and get value dynamically and able to add any number of additional fields without converting it to strongly typed.Storing data in ViewBag −ViewBag.Counties = countriesList;Retrieving data from ViewBag −string country = ViewBag.Countries;ControllerExampleusing System.Collections.Generic; using System.Web.Mvc; ... Read More

How to find and extract a number from a string in C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 10:59:04

10K+ Views

A regular expression is a pattern that could be matched against an input text. The .Net framework provides a regular expression engine that allows such matching. A pattern consists of one or more character literals, operators, or constructs.Here are basic pattern metacharacters used by RegEx −* = zero or more ? = zero or one ^ = not [] = rangeThe ^ symbol is used to specify not condition.the [] brackets if we are to give range values such as 0 - 9 or a-z or A-ZUsing Char.IsDigit()Example Live Demousing System; namespace DemoApplication{    public class Program{       static ... Read More

What are built-in message handlers in Asp.Net webAPI C#?

Nizamuddin Siddiqui
Updated on 24-Sep-2020 10:55:19

550 Views

A message handler is a class that receives an HTTP request and returns an HTTP response. Message handlers derive from the abstract HttpMessageHandler class. Message handlers provide us the opportunity to process, edit, or decline an incoming request before it reaches the HttpControllerDispatcher.Message handlers are executed much earlier in the request processing pipeline, hence they are a great place to implement cross cutting concerns in Web API. Message Handlers are nothing but a chain of classes (it may be system defined or defined by us) that sits next to the process of HTTP request and response through a pipeline.For example, ... Read More

What is parameter binding in C# ASP.NET WebAPI?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 14:21:24

1K+ Views

Binding is a process to set values for the parameters when Web API calls a controller action method.Web API methods with the different types of the parameters and how to customize the binding process.If the parameter is a simple type like int, bool, double, etc., Web API tries to get the value from the URI (Either from route data or from the Query String)if the parameter is a complex type like Customer, Employee, etc., then the Web API Framework tries to get the value from the request body.We can change this default behavior of the parameter binding process by using ... Read More

How to configure C# ASP.NET WebAPI in web.configure file?

Nizamuddin Siddiqui
Updated on 19-Aug-2020 14:20:18

1K+ Views

No we cannot configure WEB API in web.configure file.Web API supports code based configuration. It cannot be configured in web.config file.We can configure WEB API, to customize the behaviour of Web Api hosting Infrastructure and component such asRoutesFormattersFiltersDependency ResolverMessage HandlersParameterBindingRulesPropertiesServicesRoutes − The public methods of the controller are called action methods or simply actions.When the Web API framework receives a request, it routes the request to an action. To determine which action to invoke, the framework uses a routing tableroutes.MapHttpRoute(    name: "API Default",    routeTemplate: "api/{controller}/{id}",    defaults: new { id = RouteParameter.Optional } );Formatters −ASP.NET Core MVC supports ... Read More

Advertisements