Wednesday, August 8, 2018
An ASP.NET MVC can get big, and it could be overwhelming. Areas are a way to break up the application into smaller segments, A perfect candidate for an Area is the Administrative features of the site because it has multiple pages, and functionalities. So it is a good idea to segment off the Administration area to its own area (no pun intended).
To add an Area to your MVC application right-click on the project and click "Add", then select "Area..."
Type in the word "Admin" in the "Add Area" dialog
Make sure AreaRegistration,RegisterAllAreas method is called in the Global.asax.cs file so that your "Admin" area is registered
The Areas folder structure should look like this.
Now we want to create a controller for the Admin Area, right-click "Controllers" folder under "Admin" and click "Add", then select "Controller"
Select and empty controller
Call the controller "HomeController", then click "Add"
Open the HomeController.cs file under the "Contollers" folder in the "Admin" folder, right-click on the "Index" method, then click "Add View"
Except the default options for the view and select "Add"
Open the Index.cshtml and type in the following code
Now if you type in the route /Admin/Home/Index you see the message "This is from the Admin Area"
Now comes the annoying part about Areas, when you type in the route /Home/Index you will get an error that the Home controller is not unique. That is because the MVC framework puts all controllers in finds in the application into a flat list regardless of namespace.
In order for us to fix this issue we need to specify the namespace in both the "AdminAreaRegistration.cs" file and RouteConfig.cs file
First let's fix the AdminAreaRegistration.cs file, open the file and type in the following code in the RegisterArea method
Notice in the last line new string[] { "NorthwindCafe.Areas.Admin.Controllers" } we set the namespace for the Admin Area controls so that MVC looks for the controllers that are only in that namespace
Now let's do the same thing for the RouteConfig.cs file. Open the RouteConfig.cs file and change the routes.MapRoute method to the following
Now when you type in the route /Admin/Home/Index you see the Admin Index.cshtml page
And when you type in the /Home/Index route you see the application home page
Similar Posts:
To add an Area to your MVC application right-click on the project and click "Add", then select "Area..."
Type in the word "Admin" in the "Add Area" dialog
Make sure AreaRegistration,RegisterAllAreas method is called in the Global.asax.cs file so that your "Admin" area is registered
The Areas folder structure should look like this.
Now we want to create a controller for the Admin Area, right-click "Controllers" folder under "Admin" and click "Add", then select "Controller"
Select and empty controller
Call the controller "HomeController", then click "Add"
Open the HomeController.cs file under the "Contollers" folder in the "Admin" folder, right-click on the "Index" method, then click "Add View"
Except the default options for the view and select "Add"
Open the Index.cshtml and type in the following code
@{ ViewBag.Title = "Index"; } <h2>Index</h2> <h1>This is from the Admin Area</h1>
Now if you type in the route /Admin/Home/Index you see the message "This is from the Admin Area"
Now comes the annoying part about Areas, when you type in the route /Home/Index you will get an error that the Home controller is not unique. That is because the MVC framework puts all controllers in finds in the application into a flat list regardless of namespace.
In order for us to fix this issue we need to specify the namespace in both the "AdminAreaRegistration.cs" file and RouteConfig.cs file
First let's fix the AdminAreaRegistration.cs file, open the file and type in the following code in the RegisterArea method
public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional }, new string[] { typeof(NorthwindCafe.Areas.Admin.Controllers.HomeController).Namespace } ); }
Notice in the last line new string[] { "NorthwindCafe.Areas.Admin.Controllers" } we set the namespace for the Admin Area controls so that MVC looks for the controllers that are only in that namespace
Now let's do the same thing for the RouteConfig.cs file. Open the RouteConfig.cs file and change the routes.MapRoute method to the following
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, namespaces: new string[] {typeof(NorthwindCafe.Controllers.HomeController).Namespace} );
Now when you type in the route /Admin/Home/Index you see the Admin Index.cshtml page
And when you type in the /Home/Index route you see the application home page
Similar Posts:
- ASP.NET MVC: Change Admin URL to More User Friendly URL With Routing
- ASP.NET MVC; Get Rid Of The Port In The URL
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
The ISO code of Brazil is BR according to the ISO 3166 standard. The local name of the country is Brasil. Brazil capital city is Brasilia. People in Brazil speak the Portuguese language. On 7 September 1822, Brazil emerged as a sovereign political entity. In the year 09/07/1822, Brazil emerged as a sovereign political entity. The largest cities in Brazil are Brasília, Sao Paulo City, Rio de Janeiro, Salvador ; Sao Paulo, Rio de Janeiro, Belo Horizonte, Recife, Belém, Porto Alegre, Fortaleza;.
ReplyDeletehttp://www.confiduss.com/en/jurisdictions/brazil/
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeletemua ve may bay di my
chuyến bay đưa công dân về nước
vé máy bay từ đức về việt nam giá rẻ
vé máy bay từ nhật bản về việt nam
Máy bay từ Hàn Quốc về Việt Nam
khi nào có chuyến bay từ canada về việt nam
vé máy bay chuyên gia sang Việt Nam