Showing posts with label Asp.Net Core MVC. Show all posts
Showing posts with label Asp.Net Core MVC. Show all posts
Friday, April 10, 2020
Access config settings directly from class library
In the previous post we
saw on how to access the config settings
by creating a custom class and injecting it in the startup, so that it can be
used in the controllers. In this case the startup and the controller classes
are in the same project.
Access config settings using custom class
In the previous post we
saw on how to use Dependency Injection
to get the configuration options in the Controller
classes. In this post we will see another way of reading the configuration
setting using a custom class.
Saturday, September 7, 2019
Custom Models in Asp.Net Core
So far we have seen CRUD operations using a single table,
in real time scenarios we will have to deal with data from multiple tables,
like joining multiple tables and returning specific columns from each of the
tables. The default Models created using Entity
Framework Core will not help in these cases.
For example let us consider the following Employee table, which has references to Department and Country tables.
For example let us consider the following Employee table, which has references to Department and Country tables.
Friday, August 23, 2019
Entity Framework Core Where clause (Filter rows based on conditions)
To
filter a table for specific rows we need to use Where clause and specify a
filter condition in the Where clause.
To get all employees who have Age > 30 we need to use the following LINQ Query
To get all employees who have Age > 30 we need to use the following LINQ Query
Entity Framework Core Select one row from a Table
To
select a specific row from a table, we need to use the Select clause and pass
the id of the row to be obtained. Let us say we want to get the Employee with EmployeeId = 1 from the employee table
then the EF Core query will be as follows.
var Employee = _context.Employee.Select(e => e.EmployeeId == 1);
var Employee = _context.Employee.Select(e => e.EmployeeId == 1);
Thursday, May 30, 2019
Model Validation Example in Asp.Net Core
In the previous post we
saw on how to perform form field validations using Model State, in this post we will see an example of using Model
Validations in a MVC Form. Following is the Model class with the validations.
Monday, May 27, 2019
Model Validation in Asp.Net Core
Validation is an important part of any application which accepts
inputs from users, the data entered by the user needs to be validated for
integrity before it is saved in the database. Validations could be on the type
of data like int, string, decimal etc. On the format of the data like email,
SSN, Phone Number etc. Also we might need custom validation specific to the
application.
Asp.Net Core MVC Model validation provides us an easy and effective way to validate user input, using Model validation we can specify the validation parameters in the Model object and the framework will take care of validating the inputs in the View.
Asp.Net Core MVC Model validation provides us an easy and effective way to validate user input, using Model validation we can specify the validation parameters in the Model object and the framework will take care of validating the inputs in the View.
Model Binding in ASP.NET Core
Model Binding is the process by which the incoming parameters from
a HTTP request are mapped to the parameters of the corresponding Action
Methods. The parameters can be route parameters or parameters of the Action
methods. Asp.Net core framework
takes care of matching the parameters from the request with the route/action
method parameters and populated the parameters automatically.
The Framework matched the HTTP parameters with the route/action method parameters using the parameter names, when a parameter name matches it populates the property value from the request and proceeds to match the next parameter.
The Framework matched the HTTP parameters with the route/action method parameters using the parameter names, when a parameter name matches it populates the property value from the request and proceeds to match the next parameter.
Sunday, May 26, 2019
Attribute routing in Asp.Net Core MVC
In the previous post we
saw about Routing in Asp.Net Core MVC Applications, and how
to define Routing in the Applications Startup, this is called conventional
routing. Asp.Net Core MVC applications also provide us another way to define
routes called Attribute Routing.
Attribute Routing
As the name suggests Attribute Routing is defined by defining routes in action methods using a Route attribute as follows.
Attribute Routing
As the name suggests Attribute Routing is defined by defining routes in action methods using a Route attribute as follows.
Defining multiple routes in Asp.Net Core MVC
In the previous post we
saw about Routing in Asp.Net Core MVC Applications, also we
saw different ways of defining simple routing. However for more complex
application this simple routing might not be enough, we might have to define
different routing paths for each module in the application like
/Accounts/. . .
/Contacts/. . .
/Reports/. . .
/Accounts/. . .
/Contacts/. . .
/Reports/. . .
In this post we shall
see on how to define multiple routing paths in Asp.Net Core MVC.
Remember we defined a simple routing which by default was pointing to Home/Index, let us extend this by adding 2 more paths to this routing pointing to the Employee & Users controller, the route definition will be as follows.
Remember we defined a simple routing which by default was pointing to Home/Index, let us extend this by adding 2 more paths to this routing pointing to the Employee & Users controller, the route definition will be as follows.
What is Asp.Net Core MVC Routing
In traditional Asp.Net Web Forms we access a page
using the pages URL like
http://localhost/index.aspx
But in Asp.Net MVC we don’t specify the page name or extension in the URL, instead we specify the Controllers action method in the URL and the controller takes care of rendering the corresponding view. Asp.Net Core MVC uses a routing Middleware to map the incoming request to the action method. Routes can be specified either in the startup or as attributes in the Controller / Action methods.
But in Asp.Net MVC we don’t specify the page name or extension in the URL, instead we specify the Controllers action method in the URL and the controller takes care of rendering the corresponding view. Asp.Net Core MVC uses a routing Middleware to map the incoming request to the action method. Routes can be specified either in the startup or as attributes in the Controller / Action methods.
Wednesday, May 22, 2019
Injecting Service into Asp.Net Core View
In the previous posts we
saw on how register a service Dependency
Injection in the startup and inject the service to the MVC controller or Action
method. In some cases we might need to inject a service directly into the
View. A typical example is populating dropdown lists in the view.
If a view has dropdowns we cannot use the original Entity model since it will not contain the option values for the dropdown lists, in these cases we will create a custom View Model with will contain the properties from the entity model and additional lists to populate the dropdowns in the view. By injecting services to the view directly we can avoid this by injecting services directly to the view to populate the dropdown option values and continue using the entity model to display the model properties.
To inject a service directly to a view we need to use the @Inject directive. The syntax for the @Inject directive is as follows.
If a view has dropdowns we cannot use the original Entity model since it will not contain the option values for the dropdown lists, in these cases we will create a custom View Model with will contain the properties from the entity model and additional lists to populate the dropdowns in the view. By injecting services to the view directly we can avoid this by injecting services directly to the view to populate the dropdown option values and continue using the entity model to display the model properties.
To inject a service directly to a view we need to use the @Inject directive. The syntax for the @Inject directive is as follows.
Injecting Service into Asp.Net Core Action Methods
In the previous post we
saw on how to inject a service to the MVC
controller. This might not be needed always, there are cases where we will
need the service only for one action method in the controller. In this post we
shall see on how to inject a service to one of the action methods in a
controller.
The service and the service registration in the startup remains the same, instead of injecting the service in the controller’s constructor. When injecting the service in the action method we need to use the [FromServices] attribute. We will inject the service in the action method as follows.
The service and the service registration in the startup remains the same, instead of injecting the service in the controller’s constructor. When injecting the service in the action method we need to use the [FromServices] attribute. We will inject the service in the action method as follows.
Injecting Service into Asp.Net Core Controllers
In the previous example
we created an interface IUserService
and a service class UserService. We
also registered the service in the Asp.Net Core startup. In this post we will
see on how to inject this service in the constructor of MVC controller and use it to populate the view. To start with let
us create a new MVC controller and name it UserServiceController.
In the constructor of the Controller class we will pass a reference to the IUserService. Since we registered this interface with the service class in the startup, the Asp.Net runtime will inject an instance of the UserService class to the controller. Following is the code for the MVC controller.
In the constructor of the Controller class we will pass a reference to the IUserService. Since we registered this interface with the service class in the startup, the Asp.Net runtime will inject an instance of the UserService class to the controller. Following is the code for the MVC controller.
Saturday, May 18, 2019
ViewModel, Partial Views and Layouts in Asp.Net Core
| ViewModel, Partial Views and Layouts in Asp.Net Core
|
Entity Framework Core with Asp.Net MVC Core Getting Started
Layout in Asp.Net Core MVC Views
Every page on a website
will have some common elements like Header, Logo, Menu, Navigation links etc. A
layout helps to create a common view
for these re-usable part of the page and use it in each page of the
application.
A layout is also a view with .cshtml extension. The layout view defines the header, content area and the footer sections. In general re-usable content is placed in the header and footer sections, the content section varying content of the pages. The content section contains a statement @RenderBody(), which embeds the varying content of each page in the content section.
A typical layout view page looks like this.
A layout is also a view with .cshtml extension. The layout view defines the header, content area and the footer sections. In general re-usable content is placed in the header and footer sections, the content section varying content of the pages. The content section contains a statement @RenderBody(), which embeds the varying content of each page in the content section.
A typical layout view page looks like this.
Subscribe to:
Posts (Atom)