How can Add Area in razor pages on dot net core 3.0/3.1?

泄露秘密 提交于 2020-05-13 11:41:05

问题


I want to add areas in core 3.1 in Razor Pages, but Microsoft documents are for asp dot net core 2.2, and they want to create a new report in January 2020!. Does somebody know how to add areas to the core 3.1?

I searched a lot on Google but couldn't find an answer.

in _AdminLayout :

...
        <a asp-area="Channel" asp-page="Index" class="w3-bar-item w3-button w3-padding"><i class="fad fa-cog"></i>&nbsp; تنظیمات آپارات</a>
        <a asp-page="Channel/Index" class="w3-bar-item w3-button w3-padding"><i class="fad fa-cog"></i>&nbsp; تنظیمات آپارات</a>
...

asp-area="Channel" asp-page="Index" not work and even don't create url when clicked on current page refresh, but asp-page="Channel/Index" work

and in Index (channel index)

@page "{area?}"
@model Aparat.Web.Areas.Channel.Pages.IndexModel
@{
    Layout = "_AdminLayout";
}

<h1>INDDDEX</h1>

回答1:


It works well when I add areas using below steps.

Create a brand new asp.net core 3.0/3.1 Web Application,in Channel area, right click Pages folder, select Add -> New Item -> Razor Page and create Index.cshtml, you will get

Your Index.cshtml does not seems to be Razor Pages structure like mine from your provided picture.

Index.cshtml:

@page
@model WebApplication1.Areas.Channel.Pages.IndexModel

<h1>Hello from Channel Area</h1>

Index.cshtml.cs:

namespace WebApplication1.Areas.Channel.Pages
{
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
        }
    }
}

Test link:

<a asp-area="Channel" asp-page="Index">TestRedirect</a>


来源:https://stackoverflow.com/questions/59177759/how-can-add-area-in-razor-pages-on-dot-net-core-3-0-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!