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.