分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

把旧系统迁移到.Net Core 2.0 日记(7) Tag Helpers

发布时间:2023-09-06 01:49责任编辑:林大明关键词:暂无标签

Tag Helpers是Html Helpers的一种替换

比如,原来的视图模型定义是这样的:

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" })){ ???@Html.AntiForgeryToken() ???<h4>Create a new account.</h4> ???<hr /> ???@Html.ValidationSummary(true, "", new { @class = "text-danger" }) ???<div class="form-group"> ???????@Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) ???????<div class="col-md-10"> ???????????@Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) ???????</div> ???</div>

在新版MVC中,我们可以使用Tag Helper进行定义:

<form asp-controller="Account" asp-action="Register" method="post" class="form-horizontal" role="form"> ???<h4>Create a new account.</h4> ???<hr /> ???<div asp-validation-summary="ValidationSummary.ModelOnly" class="text-danger"></div> ???<div class="form-group"> ???????<label asp-for="UserName" class="col-md-2 control-label"></label> ???????<div class="col-md-10"> ???????????<input asp-for="UserName" class="form-control" /> ???????????<span asp-validation-for="UserName" class="text-danger"></span> ???????</div> ???</div>

这样方便了前端开发人员了,但我没什么感觉,因为我是Full Stack Develop

如果要用,可以参考这篇文章: http://www.cnblogs.com/TomXu/p/4496480.html

现在没有outputCache的attribute了, 取代的是 Microsoft.AspNetCore.ResponseCaching

[ResponseCache(VaryByHeader ="Accept-Encoding", Location = ResponseCacheLocation.Any, Duration = 10)]public IActionResult About(){}

如果要用VaryByQueryKeys, 要先引用Cache中间件,否则会报错  InvalidOperationException: ‘VaryByQueryKeys‘ requires the response cache middleware.

修改 Startup.cs ,在ConfigureServices 和Configure 两个方法中添加如下代码:

public class Startup{ ???... ????public void ConfigureServices(IServiceCollection services) ???{ ???????services.AddResponseCaching(); ???????????????... ???} ????public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) ???{ ???????app.UseResponseCaching(); ???????????????... ???}}

把旧系统迁移到.Net Core 2.0 日记(7) Tag Helpers

原文地址:https://www.cnblogs.com/zitjubiz/p/net_core_daily_7.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved