Provide Alias Name for Action Method in ASP.NET MVC

Nizamuddin Siddiqui
Updated on 24-Sep-2020 12:30:51

2K+ Views

ActionName attribute is an action selector which is used for a different name of the action method. We use ActionName attribute when we want that action method to be called with a different name instead of the actual name of the method.[ActionName("AliasName")]ControllerExampleusing System.Collections.Generic; using System.Web.Mvc; namespace DemoMvcApplication.Controllers{    public class HomeController : Controller{       [ActionName("ListCountries")]       public ViewResult Index(){          ViewData["Countries"] = new List{             "India",             "Malaysia",             "Dubai",             "USA",   ... Read More

Fastest Ways to Read a Text File Line by Line in C#

Nizamuddin Siddiqui
Updated on 24-Sep-2020 12:28:35

2K+ Views

There are several ways to read a text file line by line. Those includes StreamReader.ReadLine, File.ReadLines etc. Let us consider a text file present in our local machine having lines like below.Using StreamReader.ReadLine −C# StreamReader is used to read characters to a stream in a specified encoding. StreamReader.Read method reads the next character or next set of characters from the input stream. StreamReader is inherited from TextReader that provides methods to read a character, block, line, or all content.Exampleusing System; using System.IO; using System.Text; namespace DemoApplication{    public class Program{       static void Main(string[] args){       ... Read More

Return a String Repeated n Number of Times in C#

Nizamuddin Siddiqui
Updated on 24-Sep-2020 12:26:04

5K+ Views

Use string instance string repeatedString = new string(charToRepeat, 5) to repeat the character "!" with specified number of times.Use string.Concat(Enumerable.Repeat(charToRepeat, 5)) to repeat the character "!" with specified number of times.Use StringBuilder builder = new StringBuilder(stringToRepeat.Length * 5); to repeat the character "!" with specified number of times.Using string instanceExample Live Demousing System; namespace DemoApplication{    public class Program{       static void Main(string[] args){          string myString = "Hi";          Console.WriteLine($"String: {myString}");          char charToRepeat = '!';          Console.WriteLine($"Character to repeat: {charToRepeat}");          string ... Read More

Add Custom Message Handlers to the Pipeline in ASP.NET WebAPI

Nizamuddin Siddiqui
Updated on 24-Sep-2020 12:21:23

621 Views

To create a custom Server-Side HTTP Message Handler in ASP.NET Web API, we need to create a class that must be derived from the System.Net.Http.DelegatingHandler.Step 1 −Create a controller and its corresponding action methods.Exampleusing DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{    public class StudentController : ApiController{       List students = new List{          new Student{             Id = 1,             Name = "Mark"          },          new Student{             Id = ... Read More

Usage of DelegatingHandler in ASP.NET Web API

Nizamuddin Siddiqui
Updated on 24-Sep-2020 12:17:20

4K+ Views

In a message handler, a series of message handlers are chained together. The first handler receives an HTTP request, does some processing, and gives the request to the next handler. At some point, the response is created and goes back up the chain. This pattern is called a delegating handler.Along with the built-in Server-side Message Handlers, we can also create our own Server-Side HTTP Message Handlers. To create a custom Server-Side HTTP Message Handler in ASP.NET Web API, we make use of DelegatingHandler. We have to create a class deriving from System.Net.Http.DelegatingHandler. That custom class then should override the SendAsync ... Read More

What is ViewData in ASP.NET MVC

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

3K+ Views

ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa. It is valid only during the current request.Storing data in ViewData −ViewData["countries"] = countriesList;Retrieving data from ViewData −string country = ViewData["MyCountry"].ToString();ViewData does not provide compile time error checking. For example, if we mis-spell the key names we wouldn't get any compile time error. We will get to know about ... Read More

Test Chash in ASP.NET Web API

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:33:21

1K+ Views

Testing WebApi involves sending a request and receiving the response. There are several ways to test the WebApi. Here we will test the WebApi using postman and swagger. Let us create a StudentController like below.Student Modelnamespace DemoWebApplication.Models{    public class Student{       public int Id { get; set; }       public string Name { get; set; }    } }Student ControllerExampleusing DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace DemoWebApplication.Controllers{    public class StudentController : ApiController{       List students = new List{          new Student{             ... Read More

Consume ASP.NET Web API Endpoints Using C#

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:28:34

420 Views

HttpClient class provides a base class for sending/receiving the HTTP requests/responses from a URL. It is a supported async feature of .NET framework. HttpClient is able to process multiple concurrent requests. It is a layer over HttpWebRequest and HttpWebResponse. All methods with HttpClient are asynchronous. HttpClient is available in System.Net.Http namespace.Let us create an WebAPI application having a StudentController and respective action methods.Student Modelnamespace DemoWebApplication.Models{    public class Student{       public int Id { get; set; }       public string Name { get; set; }    } }Student Controllerusing DemoWebApplication.Models; using System.Collections.Generic; using System.Linq; using System.Web.Http; ... Read More

Use of Authorize Attribute in C# ASP.NET WebAPI

Nizamuddin Siddiqui
Updated on 24-Sep-2020 11:24:42

7K+ Views

Authorization is the process of deciding whether the authenticated user is allowed to perform an action on a specific resource (Web API Resource) or not. For example, having the permission to get data and post data is a part of authorization. The Authorization Process happens before executing the Controller Action Method which provides you the flexibility to decide whether we want to grant access to that resource or not.In ASP.NET Web API authorization is implemented by using the Authorization filters which will be executed before the controller action method executed. Web API provides a built-in authorization filter, AuthorizeAttribute. This filter ... Read More

Content Negotiation in ASP.NET Web API

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

Advertisements