zoukankan      html  css  js  c++  java
  • 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?}");
            });
        }
    }
  • 相关阅读:
    Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数
    HDU4522 湫湫系列故事——过年回家
    2018浙江省赛记录
    POJ3259 :Wormholes(SPFA判负环)
    上海高校金马五校赛 F题:1 + 2 = 3?
    牛客练习赛15A-吉姆的运算式(Python正则表达式瞎搞)
    ZOJ2018/4月月赛G题Traffic Light(广搜)
    luogu 3960 列队
    noip2017
    10.3 模拟赛
  • 原文地址:https://www.cnblogs.com/jizhiqiliao/p/13306742.html
Copyright © 2011-2022 走看看