Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Csharp Articles
Page 62 of 196
How to use ViewBag in ASP .Net MVC C#?
ViewBag uses the dynamic feature that was introduced in 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 vice-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. Syntax Following is the syntax for storing data in ViewBag − ...
Read MoreWhat is the significance of NonActionAttribute in ASP .Net MVC C#?
The NonAction attribute in ASP.NET MVC is used to prevent public methods in a controller from being treated as action methods. By default, all public methods in an MVC controller are accessible via URL routing. The NonAction attribute allows you to keep methods public (for internal use within the controller or class hierarchy) while blocking direct HTTP access. Syntax Following is the syntax for applying the NonAction attribute − [NonAction] public ReturnType MethodName() { // method implementation } Why Use NonAction Attribute In ASP.NET MVC, the framework automatically treats ...
Read MoreWhat are the three segments of the default route, that is present in ASP .Net MVCnC#?
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, it registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that match 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 need of mapping each URL with a physical file. Routing enables us to define ...
Read MoreWhat are the levels at which filters can be applied in ASP .Net MVC C#?
In an ASP.NET MVC application, filters can be applied at three different levels to control the behavior of controllers and action methods. These levels determine the scope of filter application, from specific methods to the entire application. Action Method Level − Applies only to a specific action method Controller Level − Applies to all action methods within a controller Global Level − Applies to all controllers and action methods in the application Filter Application Levels Global Level Controller Level ...
Read MoreWhat is the use of ChildActionOnly attribute in ASP .Net MVC C#?
The ChildActionOnly attribute in ASP.NET MVC C# restricts an action method to be accessible only through child requests from views using Html.Action() or Html.RenderAction() helpers. It prevents direct URL access to the action method, making it ideal for creating reusable partial components. Syntax Following is the syntax for using the ChildActionOnly attribute − [ChildActionOnly] public ActionResult ActionName() { // action logic return View(); } To invoke a child action from a view, use one of these helper methods − @Html.Action("ActionName", new { parameter = value }) ...
Read MoreWhat is the use of Authorize Attribute in C# Asp.Net webAPI?
The Authorize attribute in C# ASP.NET Web API is a built-in authorization filter that controls access to API endpoints. It ensures that only authenticated and authorized users can access specific resources, returning HTTP 401 Unauthorized status for unauthenticated requests. Authorization occurs before the controller action method executes, giving you control over who can access your API resources. This attribute can be applied at different levels to provide flexible access control. Syntax Following is the basic syntax for applying the Authorize attribute − [Authorize] public class ControllerName : ApiController { // Controller actions ...
Read MoreHow can we test C# Asp.Net WebAPI?
Testing ASP.NET Web API involves sending HTTP requests and receiving responses to verify that your API endpoints work correctly. There are several effective methods to test Web APIs, including using Swagger for interactive documentation and testing, and Postman for comprehensive API testing. Let us create a sample StudentController to demonstrate different testing approaches − Student Model namespace DemoWebApplication.Models { public class Student { public int Id { get; set; } public string Name { get; set; ...
Read MoreWhat is ViewData in ASP .Net MVC C#?
ViewData is a dictionary-based container in ASP.NET MVC that enables data transfer from Controller to View. It stores key-value pairs where keys are strings and values are objects, making it a flexible but loosely-typed data transfer mechanism. ViewData is valid only during the current HTTP request and provides one-way communication from controller to view. Since it uses string keys and object values, it requires explicit casting when retrieving data and does not provide compile-time type checking. Syntax Following is the syntax for storing data in ViewData − ViewData["keyName"] = value; Following is the ...
Read MoreHow to return a string repeated N number of times in C#?
There are several ways to repeat a string or character N number of times in C#. This article covers three effective approaches: using the string constructor for characters, string.Concat with Enumerable.Repeat, and StringBuilder for more complex scenarios. Using String Constructor for Characters The simplest way to repeat a single character is using the string constructor that takes a character and a count − string repeatedString = new string(character, count); Example using System; namespace DemoApplication { public class Program { ...
Read MoreWhat are some of the fastest way to read a text file line by line using C#?
There are several fast and efficient ways to read a text file line by line in C#. The most commonly used methods are StreamReader.ReadLine, File.ReadLines, and File.ReadAllLines. Each method has its own advantages depending on the use case and file size. Using StreamReader.ReadLine StreamReader is the most memory-efficient approach for reading large files since it processes one line at a time without loading the entire file into memory. This method is ideal for processing very large files where memory usage is a concern. Example using System; using System.IO; using System.Text; namespace DemoApplication { ...
Read More