简体   繁体   English

ASP.NET Core 5.0中的区域路由

[英]Area routing in ASP.NET Core 5.0

Using ASP.NET Core 5.0 MVC, the application is using an area named Counselor with a HomeController and Index action.使用 ASP.NET Core 5.0 MVC,该应用程序正在使用一个名为Counselor的区域以及HomeControllerIndex操作。

,

One link is added in Layout.cshtml to counselor.Layout.cshtml中添加了一个指向顾问的链接。

在此处输入图像描述

Also, the endpoint is already added the endpoint in Startup.cs此外,端点已添加到 Startup.cs 中的端点

在此处输入图像描述

The problem:问题:

The link created is wrong ( localhost:44308/?area=Counselor ) but it should be localhost:44308/Counselor/Home/Index创建的链接是错误的( localhost:44308/?area=Counselor )但它应该是localhost:44308/Counselor/Home/Index

在此处输入图像描述

Since I had this problem today too, this is how I solved the problem:由于我今天也遇到了这个问题,所以这就是我解决问题的方法:

  1. Make sure you have SSL enabled in your project settings (I do not know why it should be enabled) see image here .确保您在项目设置中启用了 SSL(我不知道为什么要启用它)请参见此处的图片

  2. Try this code in Startup.cs:在 Startup.cs 中尝试此代码:

    endpoints.MapAreaControllerRoute(
        name: "Counselor",
        areaName: "Counselor ",
        pattern: "Counselor/{controller=Home}/{action=Index}/{id?}");
  1. Then don't forget to add [Area("Counselor")] at the beginning of the controller like this:然后不要忘记在 controller 的开头添加 [Area("Counselor")] ,如下所示:
    namespace Project.Controllers
    {
        [Area("Counselor")]
        public class HomeController : Controller
        {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM