为了向导航栏添加新条目,请先找到原有导航栏的 HTML 代码:

    Views/Shared/_Layout.cshtml

    添加你的条目,不要指向 Home 控制器,而要改为指向 Todo:


    Update the layout

    The layout file at Views/Shared/_Layout.cshtml contains the “base” HTML for each view. This includes the navbar, which is rendered at the top of each page.

    To add a new item to the navbar, find the HTML code for the existing navbar items:

    Add your own item that points to the Todo controller instead of Home:

    The and asp-action attributes on the <a> element are called tag helpers. Before the view is rendered, ASP.NET Core replaces these tag helpers with real HTML attributes. In this case, a URL to the /Todo/Index route is generated and added to the <a> element as an href attribute. This means you don’t have to hard-code the route to the TodoController. Instead, ASP.NET Core generates it for you automatically.

    If you’ve used Razor in ASP.NET 4.x, you’ll notice some syntax changes. Instead of using to generate a link to an action, tag helpers are now the recommended way to create links in your views. Tag helpers are useful for forms, too (you’ll see why in a later chapter). You can learn about other tag helpers in the documentation at .