ASP. NET Core create areas[zone]

Source: Internet
Author: User

in ASP. NET Core VS2015, we found that there are a lot of less convenient places, such as right-click Add View, go to the attempt page and other function diagram is missing, although we can use the toolbar's custom command, the two right-click menu added, but it seems that the gray is not available.

In fact, this is good, through the manual creation, let us deeply understand the relationship between MVC and routing, many people think that the bottom of things are tall, such as a reference to the assembly, many people feel that the cow can not do, in fact, the operation of the industry has specialized, do the process of the feeling of nuclear power is very cattle, nuclear power also feel , but by creating MVC and routing manually, in addition to giving us a deep understanding of MVC, we have met some of the friends who have this idea in some way.


Add Configuration Area

We added zones in previous versions, usually right-click → add zone, and in the created zone directory, the routing configuration from the zone. As follows:




This is actually very convenient, but in VS2015, there is no area option in the Add option, we can only create the area manually:
① Add a Zone folder areas and add a folder under the zone file Sysmanage


② Create a new two folder under Sysmanage Controllers (Controller) and Views (view)



③ Add a controller AccountController


Note: The Areaattribute name is identical to the folder name

④ we are in the Views folder, create a new folder account (consistent with the controller name), and then add the view index.cshtml



⑤ under Startup.cs, add the zone routing configuration routes. MapRoute ("Arearoute", "{area:exists}/{controller}/{action=index}/{id}");



So, we're done with the area, so let's test it out:




Add Taghelper

Modified in the previous version, for _viewstart.cshtml Everyone is familiar with, is a view file in the time of the launch file, will be executed before all view (. cshtml) is executed, such as set layout = "~/views/shared/ _layout.cshtml "; Such a statement can only be written once in _viewstart.cshtml.
In this version, a new _viewimports.cshtml file is added, and the main function of this file is to provide namespaces for all other views. In previous MVC projects, this feature was provided by the Web. config file in the View folder, and because Web. config no longer exists, the global namespace is provided by _viewimports.cshtml.

We don't care about this file, let's add a taghelper tag:




We found that taghelper seemingly not, we can test through the browser, in fact, in VS2015, if possible, here should be bold, we look at the system comes with the sample taghelper:



What is this for? is because we did not add a reference to Taghelper. Let's add a reference @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers



Let's take a look at the two taghelper tags we just created:


We found out that Taghelper already worked, and we just talked about _viewimports.cshtml is a file that provides a global namespace, so we don't need to add a reference to each view file, we add a global reference, Just let each attempt page refer to Taghelper.

We add a _viewimports.cshtml under the Views folder of areas:

and add Taghelper references:


We delete account\index.cshtml's taghelper reference, we look at:



Similarly, we can also add a reference to some model namespaces in _viewimports.cshtml:


in ASP. NET Core VS2015, we found that there are a lot of less convenient places, such as right-click Add View, go to the attempt page and other function diagram is missing, although we can use the toolbar's custom command, the two right-click menu added, but it seems that the gray is not available.

In fact, this is good, through the manual creation, let us deeply understand the relationship between MVC and routing, many people think that the bottom of things are tall, such as a reference to the assembly, many people feel that the cow can not do, in fact, the operation of the industry has specialized, do the process of the feeling of nuclear power is very cattle, nuclear power also feel , but by creating MVC and routing manually, in addition to giving us a deep understanding of MVC, we have met some of the friends who have this idea in some way.


Add Configuration Area

We added zones in previous versions, usually right-click → add zone, and in the created zone directory, the routing configuration from the zone. As follows:




This is actually very convenient, but in VS2015, there is no area option in the Add option, we can only create the area manually:
① Add a Zone folder areas and add a folder under the zone file Sysmanage


② Create a new two folder under Sysmanage Controllers (Controller) and Views (view)



③ Add a controller AccountController


Note: The Areaattribute name is identical to the folder name

④ we are in the Views folder, create a new folder account (consistent with the controller name), and then add the view index.cshtml



⑤ under Startup.cs, add the zone routing configuration routes. MapRoute ("Arearoute", "{area:exists}/{controller}/{action=index}/{id}");



So, we're done with the area, so let's test it out:




Add Taghelper

Modified in the previous version, for _viewstart.cshtml Everyone is familiar with, is a view file in the time of the launch file, will be executed before all view (. cshtml) is executed, such as set layout = "~/views/shared/ _layout.cshtml "; Such a statement can only be written once in _viewstart.cshtml.
In this version, a new _viewimports.cshtml file is added, and the main function of this file is to provide namespaces for all other views. In previous MVC projects, this feature was provided by the Web. config file in the View folder, and because Web. config no longer exists, the global namespace is provided by _viewimports.cshtml.

We don't care about this file, let's add a taghelper tag:




We found that taghelper seemingly not, we can test through the browser, in fact, in VS2015, if possible, here should be bold, we look at the system comes with the sample taghelper:



What is this for? is because we did not add a reference to Taghelper. Let's add a reference @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers



Let's take a look at the two taghelper tags we just created:


We found out that Taghelper already worked, and we just talked about _viewimports.cshtml is a file that provides a global namespace, so we don't need to add a reference to each view file, we add a global reference, Just let each attempt page refer to Taghelper.

We add a _viewimports.cshtml under the Views folder of areas:

and add Taghelper references:


We delete account\index.cshtml's taghelper reference, we look at:



Similarly, we can also add a reference to some model namespaces in _viewimports.cshtml:


ASP. NET Core create areas[zone]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.