asp.net core 使用TagHelpers

 

 

 第一章

 

a标签

 

引入

 

@using Webgentle.BookStore.Models;

@addTagHelper *,Microsoft.AspNetCore.Mvc.TagHelpers

 

 

 

 

使用

 

 

 跟控制器对应的

  <a class="nav-link text-dark" asp-controller="Home" asp-action="Index">Home</a>

 

 

 

 

 

结果

 

 

 

 

扩展

在Start文件,这样可以在他前面添加bookAPP,还是有效的

 

 

 

 

如何使用参数

 <a asp-controller="book" asp-action="getbooks" asp-route-id="@book.Id"  class="btn btn-sm btn-outline-secondary">View</a>
                            </div>
asp-route-id 这个id是和控制器方法参数名对应的

 

 

 

 

 

 

其他的可以自己去查阅了

asp-host

asp-protocol

Attrubute 等等




自定义路由

 

 

 

 

 

 

 

 

 

第二章

图像标签

<img src="~/image//logo.jpg" width="30" height="30" asp-append-version="true"/>

asp-append-version 静态文件会更新,更改图片的时候,后面有哈希值,而不会读到缓存之前的图片

 

 

 

 

 

 

第三章

环境标记帮助器

 

 

 

 

 

 <environment names="Development">
            <h1>Development</h1>
        </environment>

        <environment names="Production">
            <h1>Production</h1>
        </environment>

 

 

 

可以加逗号2个环境

 

 

又或者这样,include

 

exclude

 

 

 

 

 

 

例子

 

 

 

 

 

 

 

第四章

link表示器

 

 如何href不生效,就读取我们本地的,asp-fallback-href

 

 

 

 

第五章

表单标签助手

form  tag

 

 

addNewBook.cshtml

@{
    ViewData["Title"] = "AddNewBook";
}

@model BookModel




<div class="container">

    <h3 class="display-4 mt-4">Add new book</h3>

    <form method="post" asp-action="AddNewBook" asp-controller="Book">

    <div class="form-group">
        <label asp-for="Title"></label>
        <input type="text" asp-for="Title" class="form-control"/>
    </div>
    
    <div class="form-group">
        <label asp-for="Author"></label>
        <input type="text" asp-for="Author" class="form-control" />
    </div>

    <div class="form-group">
        <label asp-for="Description"></label>
        @*<input type="text" asp-for="Description" class="form-control"/>*@
            <textarea asp-for="Description" class="form-control" ></textarea>
    </div>


    <div class="form-group">
            <label asp-for="TotalPages"></label>
            <input  asp-for="TotalPages" class="form-control" />
    </div>

    <div class="form-group">
        <input  type="submit" value="Add book" class="btn btn-primary"/>
    </div>

    
</form>
</div>

 

 

bookController.cs

 

posted @ 2023-03-15 14:12  漫漫长路</>  阅读(59)  评论(0编辑  收藏  举报