藉由前几篇简单操作得知网址路由寻访
可以跳至Controller做相应Action Method执行
预设路由规则:
域名/{Controller类}/{Action方法}
控制器一些重点
1.必须继承自Controller Class。
2.必须是public修饰,若非public则会被视作一般的method。
3.类别(档案)名称建议以Controller结尾,若不想或没有这样取名则要加上[Controller]附加属性在class之上。
附加属性
[NonAction]
当你Controller中有些方法不想被视为可以被访问的action method可以加上[NonAction],或者也可以调整为private修饰。
[HttpGet]
预设若不加就是采用HttpGet
使用GET方法向Server发出请求(QueryString ->url?Key1=Val1&Key2=Val2)
隐密性较低,传输量较小。
[HttpPost]
使用POST方法向Server发出请求(传输资料是存在讯息主体常搭配Form表单)
传输辆较无限制,也较隐密。
[HttpDelete]
向Server发出删除指定资源请求
[HttpPut]
向Server发出新增或更新指定资源请求
有提及到类别(档案)名称建议以Controller结尾
但有时不想或没有这样取名则要加上[Controller]附加属性在class之上。
这里示范不透过新建控制器方式来新增Controller class
这里新建一个class命名为MyTest
让其继承自Controller并增加一个附加属性[Controller]也可以被视为一个控制器类
於Controller的action method中
若想要获取Cookie , Headers Form 等资料时
可以透过Request.Query["键值"]
或者Request.Form["键值"]
来获取
Controller在做一些Action method呼叫时候
若有从client传来的资料都建议要encode处理过後
以免有XSS攻击风险
附上一个简单的程序范本
StudentController.cs
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace Net5MvcApp1.Controllers
{
public class StudentController : Controller
{
//http://localhost:40535/student/show
public IActionResult Show()
{
return View();
}
/// <summary>
/// 无参数方法
/// http://localhost:40535/student/ShowStr
/// </summary>
/// <returns></returns>
public string ShowStr()
{
return "你好";
}
public int Num()
{
return 500;
}
//http://localhost:40535/student/showwithparam/?name=李四
//http://localhost:40535/student/showwithparam/?name=%E6%9D%8E%E5%9B%9B
public string ShowWithParam(string name)
{
return string.Format("你好{0}", name);
}
//http://localhost:40535/student/showwithparams/?name1=张三&name2=李四
//http://localhost:40535/student/showwithparams/?name1=%E5%BC%B5%E4%B8%89&name2=%E6%9D%8E%E5%9B%9B
public string ShowWithParams(string name1, string name2)
{
return string.Format("你好{0},和{1}", name1, name2);
}
//http://localhost:40535/student/ShowWithId/12
//http://localhost:40535/student/ShowWithId?id=12
public string ShowWithId(string id)
{
return string.Format("ID:{0}",id);
}
//防止恶意输入
//http://localhost:40535/student/ShowWithEncode?name=<script>alert("XSS attack")</script>
//Before
//http://localhost:40535/student/ShowWithEncode?name=%3Cscript%3Ealert(%22XSS%20attack%22);%3C/script%3E
//<script>alert("XSS attack");</script>
//After
//http://localhost:40535/student/ShowWithEncode?name=%3Cscript%3Ealert(%22XSS%20attack%22)%3C/script%3E
//<script>alert("XSS attack")</script>
public string ShowWithEncode(string name)
{
//return name;
return HtmlEncoder.Default.Encode(name);
}
}
}
Controller在返回View上有一些不同方式
新增好另一个Test的MVC控制器并产生预设视图
第一种.预设View(action method名称是舍麽就去抓对应view)
第二种.回传指定名称的View ,这里return View("视图名称") 可
不写完整路径,更可以省略副档名,前提是
该View必须存在於Views目录的当前Controller目录当中。
也可以用来做类似权限判断导向不同页面的机制
第三种.不在当前Controller目录下要回传指定视图完整路径(必须存於/Views目录之下)
TestContoller.cs程序码
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Net5MvcApp1.Controllers
{
public class TestController : Controller
{
public IActionResult Index(/*string viewName*/)
{
return View("~/Views/Home/Index.cshtml");
//if (viewName == "index" )
//{
// return View();
//}
//else
//{
// return View("News");
//}
//return View("News");
}
}
}
本篇同步发表至个人部落格
https://coolmandiary.blogspot.com/2021/07/net-core11controller.html
<<: AI ninja project [day 11] 图片分类(1)
此篇会介绍 box-model 是什麽以及如何使用,最後会分享几个使用技巧以及常见的迷思,建议阅读前...
Spinner选项的字体大小及背景都是可以更改的! 但Spinner的字体样式及背景无法像Textv...
今天要测试永丰提供的kbars API, (1)老规矩先进行汇入使用的库以及登入的动作, 今天使用的...
此篇会介绍 Bootstrap 客制化 sass 原始码架构,着重在如何使用原始码来客制化自己的 ...
在写网页时,前端的工程师需要了解如何与後端或是其他的第三方服务沟通,在看了文件後,除了直接实作外,另...