• ASP.NET Core MVC中Area的使用


    首先,在Solution Explorer下新建一个Areas文件夹。

    然后右击该文件夹,选择“Add” -> “Area”,新建Area。如下所示:

    然后分别设置其相应的Controller和View。

    AnotherController.cs

    [Area("AnotherArea")]
    public class AnotherController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }

    Index.cshtml

    @{
        ViewData["Title"] = "Another Page";
    }
    
    <div class="text-center">
        <h1 class="display-4">Another Index</h1>
    </div>

    在这里新建一个Controller(ShowMessageController),然后将默认的路由导向该Controller。

    ShowMessageController.cs

    public class ShowMessageController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }

    Index.cshtml

    @{
        Layout = null;
    }
    
    <div class="text-center">
        <h1 class="display-4">ShowMessage Index</h1>
        <ul>
            <li><a class="dropdown-item" asp-area="MyAreaTest" asp-controller="ShowTest" asp-action="Index">Show Test</a></li>
            <li><a class="dropdown-item" asp-area="AnotherArea" asp-controller="Another" asp-action="Index">Another</a></li>
            <li><a class="dropdown-item" asp-area="MyThirdArea" asp-controller="Third" asp-action="Index">Third</a></li>
        </ul>
    </div>

    再在Startup.cs中设置路由。

    public class Startup
    {
    
       // ...
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // ...
    
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapAreaControllerRoute(
                    name: "MyAreaTest",
                    areaName: "MyAreaTest",
                    pattern: "MyAreaTest/{controller=ShowTest}/{action=Index}/{id?}");
    
                endpoints.MapAreaControllerRoute(
                    name: "AnotherArea",
                    areaName: "AnotherArea",
                    pattern: "AnotherArea/{controller=Another}/{action=Index}/{id?}");
    
                endpoints.MapAreaControllerRoute(
                    name: "MyThirdArea",
                    areaName: "MyThirdArea",
                    pattern: "MyThirdArea/{controller=Third}/{action=Index}/{id?}");
    
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=ShowMessage}/{action=Index}/{id?}");
            });
        }
    }
  • 相关阅读:
    hdu1561--树形dp<依赖背包>
    hdu--1520--树形dp<写起来就是深搜啊>-<滚动数组优化>
    hdu--1595-另类最短路
    hdu--1599--最小环<会加深你对floyd的理解>
    hdu--1851--尼姆博弈&&巴什博弈<也有人用了sg可惜我还不懂>
    hdu--4920--原来一直写了速度慢的矩阵乘法
    hdu--4912--终于解脱了
    hdu--4947--我太天真了
    hdu--2576--高中数学..
    hdu--2579--第二次与女孩的约会
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13306742.html
Copyright © 2020-2023  润新知