.NET Core第21天_FormTagHelper的使用_防止跨站请求设置方式

FormTagHelper : 为.net对html原生的封装, 预设若没指定method则是采用get提交。

新建好FormController.cs

using Microsoft.AspNetCore.Mvc;
using Net5App6.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Controllers
{
    public class FormController : Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Add(FormViewModel model)
        {
            if (ModelState.IsValid)
            {
                string name = model.Name;
                int age = model.Age;
                string remark = model.Remark;
            }
            return View(model);
        }
    }
}

FormViewModel.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Models
{
    public class FormViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string Remark { get; set; }
    }
}

Index.cshtml

@model FormViewModel
<form method="post" asp-controller="Form" asp-action="Add">
    <div>
        <label asp-for="Name"></label>
        <input type="text" asp-for="Name" />
    </div>
    <div>
        <label asp-for="Age"></label>
        <input type="text" asp-for="Age" />
    </div>
    <div>
        <label asp-for="Remark"></label>
        <textarea asp-for="Remark"></textarea>
    </div>
    <div>
        <input type="submit" value="提交" />
    </div>
</form>

https://ithelp.ithome.com.tw/upload/images/20210921/20107452YLf2VEItIc.png

防止跨站请求
预设情况FormTagHelper会在View上产生隐藏的请求用Token
但需要和HTTP Post的Action method上标注[ValidateAntiForgeryToken]才会生效,可以防止跨站请求伪造。

using Microsoft.AspNetCore.Mvc;
using Net5App6.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Controllers
{
    public class FormController : Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Add(FormViewModel model)
        {
            if (ModelState.IsValid)
            {
                string name = model.Name;
                int age = model.Age;
                string remark = model.Remark;
            }
            return View(model);
        }
    }
}

https://ithelp.ithome.com.tw/upload/images/20210921/20107452nlXQ1UEtoZ.png

https://ithelp.ithome.com.tw/upload/images/20210921/20107452Xjv9zr7bcY.png

自订路由命名 asp-route
通常若不想多写太多asp-controller 、asp-action等等属性,想要简写时候可以用这类功能,
但要注意 路由名称name不能够有重覆,专案中要唯一。

这里扩充一个action method 名称Edit

using Microsoft.AspNetCore.Mvc;
using Net5App6.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Net5App6.Controllers
{
    public class FormController : Controller
    {
        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        [Route("/Form/EditData",Name ="R_E")]
        public IActionResult Edit(FormViewModel model)
        {
            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Add(FormViewModel model)
        {
            if (ModelState.IsValid)
            {
                string name = model.Name;
                int age = model.Age;
                string remark = model.Remark;
            }
            return View(model);
        }
    }
}

Index.cshtml

@model FormViewModel
@*<form method="post" asp-controller="Form" asp-action="Add">*@
<form method="post" asp-route="R_E" >
    <div>
        <label asp-for="Name"></label>
        <input type="text" asp-for="Name" />
    </div>
    <div>
        <label asp-for="Age"></label>
        <input type="text" asp-for="Age" />
    </div>
    <div>
        <label asp-for="Remark"></label>
        <textarea asp-for="Remark"></textarea>
    </div>
    <div>
        <input type="submit" value="提交" />
    </div>
</form>

https://ithelp.ithome.com.tw/upload/images/20210921/20107452OON5MnH42j.png

於专案中之前额外的范例练习我有在AnchorController
写一个路由模板, 名称为stu_show
https://ithelp.ithome.com.tw/upload/images/20210921/20107452kZTVES1FZR.png

这里我把他故意改成一样叫stu_show测试
https://ithelp.ithome.com.tw/upload/images/20210921/201074526sTpQRDudn.png

当一运行即可看到错误
https://ithelp.ithome.com.tw/upload/images/20210921/20107452DfljwIjXbM.png

重导向路由
route-returnurl:可以允许指定重新跳转到指定URL。
比方登入错误就重新跳转到登入页面、首页或上一页。

本篇已同步发表至个人部落格
https://coolmandiary.blogspot.com/2021/08/net-core21formtaghelper.html


<<:  Day 16:专案03 - PTT 八卦版爬虫01 | cookie

>>:  Code Generator 结构

Day 29 - 在 VyOS 上设定 GRE

那今天,我们来讲一下在 VyOS 上设定 GRE Tunnel 的指令 其实跟昨天的大同小异。 se...

D24-(9/24)-统一(1216)-刚开始学习买股票时就一直持有的股票

注:发文日和截图的日期不一定是同一天,所以价格计算上和当日不同,是很正常的。 声明:这一系列文章并无...

[DAY27]将Line讯息存入资料库(01)

再来就是我们要利用Line来记录我们的资料了,以下程序码我放在一个新增程序档 import psyc...

Day2 HTML

每件事情的开头一定都有属於他自己的原因,想达成的目标、想解决的问题、後面的文章会绕着三大主轴在运转:...

【Day18-音调】我们怎麽听出来不同音高的差别?——python中对於声音频率的处理

昨天我们针对声音讯号的基本处理做了一些简单的介绍,知道了声音是一连串随着时间变化的讯号所组成的,同时...