Simple implementation of adding a region in Asp. Net Core, asp. netcore

Source: Internet
Author: User

Simple implementation of adding a region in Asp. Net Core, asp. netcore

The use area can effectively isolate services, and various services and labor division can be more flexible. The enable area in Asp. Net Core is also very simple.

 

Procedure:

1. Add a region route in Startup. cs:

            app.UseMvc(routes =>            {                routes.MapRoute(                    name: "area",                    template: "{area:exists}/{controller=Home}/{action=Index}/{id?}");                routes.MapRoute(                    name: "default",                    template: "{controller=Home}/{action=Index}/{id?}");            });

2. Create the Areas folder under the project and add the corresponding Controllers, Views, and Models folders:

3. Add HomeController under Controllers. cs, (I usually recommend that you create a BaseController before adding the actual Controller, and all other controllers are inherited from the BaseController. In this way, during actual development, many configurations and common methods can be used more easily), and apply the attribute [Area ("name")] on the Controller. If you set the attribute in BaseController. All controllers inherited from BaseController do not need to be set repeatedly:

    [Area("Mobile")]    public class BaseController : Controller    {        // GET: /<controller>/        public IActionResult Index()        {            return View();        }    }

4. Add the Area attribute to the backend redirection:

        public IActionResult Test()        {            return RedirectToAction("index", "home", new { Area = "mobile" });        }

5. The Area attribute must also be added to the front-end link generation:

@{    ViewData["Title"] = "About";}

 

OK. Other precautions are similar to Asp. Net in the past.

 

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.