In ASP.NET MVC, every public method of controller is accessible through the url regardless of return type. If we create any public method in the controller which is not intend to response as like action method. But it also accessible through the browser ULR. To overcome this scenario, the NonAction attribute has introduced
“All methods in MVC are treated as a action method. “
Purpose of using :
As per above statement, all public methods of a controller class are basically treated as action methods in Asp .Net MVC. The methods which are
By using this we changing the default behavior.
Example
Both GetUserDetail are treated as action methods.
public ActionResult GetUserDetail(){ // some code inside here return View(); }
public void GetUserDetail(){ // some code inside here }
So overcome this we are using NonAction Attribute method
[NonAction] public void GetUserDetail(){ // some code inside here }
[NonAction] public ActionResult GetUserDetail(){ // some code inside here return View(); }
Now above two methods are treated as Non action method.
The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. In simple, ChildActionOnly attribute make controller action method to consider as non-action methods (It treated as normal method).
Where to use ChildActionOnly?
ChildActionOnly it related with partial views but it is not compulsory. If decided not to access through url then we may declare action method as ChildActionOnly.
This attribute used to prevent the action method being invoked as a result of a user request, that is it prevent from accessing via url.
Creating a Child Action Method
Let we discuss with example,
public class ChildActionTestingController : Controller {
[ChildActionOnly] public ActionResult GetUserDetails(int userId) { //Your Logic Here !!! return PartialView(“GetUserDetails”, null); } }
Here GetUserDetails method is non-action method. The GetUserDetails method cant able to access by URL like ChildActionTesting/GetUserDetails.
After declare GetUserDetails as childactiononly attribute, then it can’t allow to access through url. If you try to access, it show the error msg like below. It will treat as a normal method.
“The action ‘GetUserDetails’ is accessible only by a child request.”
Asynchronous controller enables us to create asynchronous action methods in our application. This allows us to perform long running operation without making the running thread idle. Asynchronous action methods are useful when an action must perform several independent long running operations.
Synchronous
In Synchronous, if you making request, then you will have to wait till the response, you may not do other process until it response.
Asynchronous
In Asynchronous , if you making request, then you don’t need to wait for the response and we can perform any other process/task. Whenever the response arrive, you can receive in call back delegate.
What is Thread starvation in WebServer ?
From above, the request from Request A, Request B are accepted and processing but Request C not able to get the thread because the size of the pool is 2 so this situation is called thread starvation.
In this case we may get Server Busy Error
As per MSDN, When a request arrives, a thread from the pool is dispatched to process that request. If the request is processed synchronously, the thread that processes the request is blocked while the request is being processed, and that thread cannot service another request.
This might not be a problem, because the thread pool can be made large enough to accommodate many blocked threads. However, the number of threads in the thread pool is limited. In large applications that process multiple simultaneous long-running requests, all available threads might be blocked. This condition is known as thread starvation
If you have multiple long-running requests, all available threads might be blocked and the Web server rejects any additional request with an HTTP 503 status
(Server Too Busy).
To overcome this, ASP.NET MVC provide asynchronously call in controller/action
Here let we see the asynController
How to convert controller to asynchronously
To convert a synchronous action method to an asynchronous action method involves the following steps: 1) Before we deriving the controller from Controller, now we want to derive it from AsyncController. Controllers that derive from AsyncController enable
ASP.NET to process asynchronous requests, and they can still service synchronous action methods.
Example
We want to apply “Async” before controller like
public class AsynchronuosController : AsyncController
The “OutstandingOperations” property tells ASP.NET about how many operations are pending. It is needed because ASP.NET dont known how many operations were initiated by the action method or when those operations are complete. When OutstandingOperations property is zero, ASP.NET completes its process by(asynchronous operation) calling the Completed method.
Scaffolding is a Templates for Create, Delete, Edit ,Details. It will reduce the time for developer for creating controller,view for the specific model
This is a Scaffolding package for ASP.NET which is installed via NuGet using ‘Install-Package MvcScaffolding’ command
Using Templates
Step : 1
Create Sample web project
Step : 2
Create custom model as like below
Step : 3
Here we going to install the scaffolding in our project. GoTo Tools –> Library Package Manager –> Package Manager Console as shown below to install the scaffolding
To install Scaffolding, type below command in below console cmd : Install-Package MvcScaffolding
Step : 4
Next creating template against custom model by using below command
cmd : Scaffold controller EmployeeModel
After executing the command in console, it will create the template for CRUD Operation as shown below
Output :
After generation of Views, run the application. Now u can perform CURD Operation without extra code.
Really, I spend more time to understand Poco and DTO with a lot of confusion and questions. After understanding I decide to post immediately to my blog.
What is POCO?
POCO stands for Plain Old CLR Object.It provides freedom to define an object model in which objects does not inherit from specific base classes
POCO data classes, also known as persistence-ignorant objects, it refers to an object do not have any persistence concerns
It mainly has a control over implementation and design
A POCO is a BO (Business object). We can implement validation and any other business logic can occur here.
POCO have state and behavior.
What is Persistence ignorance in POCO
It means (layers) it does not depend on the design of the database, IE., type of database, type of database object.
Example for POCO Model
public class Customer {
public int CustomerID
{ get; set; }
public string CustomerName
{ get; set; }
public string CustomerGender
{ get; set; }
}
What is DTO?
Stands for Data Transfer Object, its Main purpose is to transfer data.
It just stores data. It is a lightweight container used for transferring data between layers.
There will be no effect on application if entity changed or modified (based on the Database structure)
Example for DTO Model
public Customer()
{
CustomerID = Int_NullValue;
CustomerName= String_NullValue;
CustomerGender = String_NullValue;
Address = String_NullValue;
} Difference between POCO and DTD
POCO has state and behavior, but DTD has only state (it does not contain behavior IE., method)
POCO describes an approach to programming, where DTO is a pattern that is used to move data using objects.
@Html.TextBox(“txtName”, new{@class = “txtbox”}) – It will not apply the style to the textbox
From the above code new{@class = “txtbox”} want to be third parameter , if you not going to pass the value as second parameter then we need pass null/Empty value.
For example, we have a login form with “sign in”,”sign up” and “cancel” button. In this scenario, “sign in” will redirect/do some action,same like sign up and cancel. Lets we discuss how to handle multiple button in same form (calling different action method).
Attributes are classes that allow you to add additional information to elements of your class structure.
What is Bind Attributes ?
In ASP.NET MVC View that accepts user input and post information to a server. Bind Attribute allow an option to restrict the properties(ie., model) that are allowed to be bound automatically.
Where can we use Bind Attributes ? :
Scenario :
For example we having model/properties for binding the EMP DETAILS as shown below
Model :
public class EmployeeDetail { public string EmpName {get;set;} public string EmpAddress {get;set;} public string Department {get;set;} }
From above some employee have rights to enter Department but some employee not have such rights to add Department. In such scenario we can easily do it form level by hiding the Department Details from the page/Separate page, In this scenario we can use Bind Attribute to eliminate this property to update in Model/Property/DB.
Example : We can achieve this in two ways
1) Handel in Action Level
2) Handel in Model Level
Let we discuss about Action Level :
In Action Level, we will restrict the property while receiving to method ( it will not filter in model)
controller :
In controller we can restrict particular property (department) as shown below
public class EmployeeDetailController : Controller {
Click insert button to view in debug mode, as shown In below the model restrict to bind the value for the Department.
Let we discuss about Model Level Restriction :
We can handel in Model as shown below
[Bind(Exclude = “Department”)] public class EmployeeDetail { public string EmpName{get;set;} public string EmpAddress{get;set;} public string Department{get;set;} }
NOTE :
Attribute will be executed before the action
If we place [Bind(Exclude = “Department”)] above the class it will exclude Department from other class inside the particular Model page
If we place it inside the class it will applicable to particular class
In MVC we have default folders (view, model, controller…) structure, If application grew larger, then it will be complicated to maintain a modules and structure logic for the file in the solution.
To overcome this, MVC provide areas,Using MVC area has its own folder structure which allow us to keep separate controllers, views, and models…
Creating Areas
Step 1:
To add an area to application, right-click on the project —> select Add —> Area option as shown below.
Step 2:
Enter the Module/Areas Name in the “Add name Prombt” as shown below
Click ADD button to create structure(Areas) as shown below
Same way we can create more areas based on our modules…
Step 3:
Create Controller by right clicking the controllers folder then create view , as shown in below
Step 4:
Here we start discuss about “How to call Module_1(Areas) from main project”.
Here i am creating actionlink for calling the view inside the Module_1
@Html.ActionLink(“Go to Module-1 Index”,”Index”,”Module_1″,new {area =”Module_1″},null)
“Go to Module-1 Index” : Name of link
Index : Name of the Action
Module_1 : Name of the controller
new {area =”Module_1″} : Name of the area
Step 5 : Run the Application
OUTPUT :
By clicking the link, it will redirect to Module_1 (inside the Areas) as shown below
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.