惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
F
Fortinet All Blogs
H
Heimdal Security Blog
S
Schneier on Security
L
LangChain Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
J
Java Code Geeks
博客园 - 【当耐特】
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
I
InfoQ
Recorded Future
Recorded Future
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
腾讯CDC
C
Check Point Blog
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
V2EX - 技术
V2EX - 技术
T
Threatpost
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿

博客园 - 取经路上

IIS 下发布SignalR 访问接口进不去处理 Linux下部署.Net 应用程序和Web应用程序 CentOS 7 配置启动 手动编译的 nginx CentOS 7 nginx 安装 sticky模块 关于SignalR并发量测试 C# 调用迅雷aplayer播放器的遇到的问题总结 C# 采用HttpWebRequest 、WebClient和HttpClient下载https的文件异常问题 C# 判别系统版本以及Win10的识别办法 MVC 前后台传值 MVC基础关键点 sqlserver 数据库、日志文件收缩 笔记 vue-cli启动报错问题: IE6无法获取class属性 windows server 2008 r2 datacenter 共享服务找不到网络路径解决办法 在Visual Studio 中的监视窗口中监视Com对象变量 删除GitHub上项目中的某个文件 转 WPF MVVM 循序渐进 (从基础到高级) 服务器未能识别 HTTP 头 SOAPAction 的值: http://tempuri.org/QueryUserName。
MVC ActionResult 视图模型
取经路上 · 2021-09-02 · via 博客园 - 取经路上

ViewResult,ContentResult,RedirectResult,RedirectToRouteResult,FileContentResult,JsonResult,HttpStatusCodeResult,PartialViewResult

[HttpGet]
        public ActionResult Login()
        {

            return View();//返回一个视图页面
        }
[HttpGet]
        public ActionResult Login1()
        {

            return Content("hello");//返回一个字符串
        }
[HttpGet]
        public ActionResult Login()
        {
       //Respones.Redirect 一个封装 
            return Redirect("http://wwww.baidu.com");//重定向
        }
//通过路由跳转到控制器
[HttpGet]
public ActionResult RedirectToAction() { //跳转到Action return RedirectToAction("Login1"); } [HttpGet] public ActionResult RedirectToAction2() { //跳转到指定控制器的Action return RedirectToAction("Index","Student"); }

File()返回文件,有两个参数,一个文件名,一个是内容类型,向客户端输出文件

有多个方法重载

[HttpGet]
public ActionResult GetFile(string filename)
{
string upload = "~/upload";
return File($@"{Server.MapPath(upload)}\{filename}", "image/png");
}

可以在Html中使用标签访问,也可以直接get请求获取图片

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <img src="Home/GetFile" alt="Al" width="100" height="40" />
</body>
</html>

 上传文件控制器

string upload = "~/upload";
        [HttpPost]
        public ActionResult UploadFiles(HttpPostedFileBase file)
        {
            if (!Directory.Exists(Server.MapPath(upload)))
            {
                Directory.CreateDirectory(Server.MapPath(upload));
            }
            var filename = DateTime.Now.Ticks + file.FileName;
            file.SaveAs($@"{Server.MapPath(upload)}\{filename}");
            return Content(filename);
        }
    <form action="/Home/UploadFiles" enctype="multipart/form-data" method="post">
        <input type="file" name="file" /><button>上传文件</button>
    </form>
    

向客户端输出Json格式数据 

        public ActionResult Json()
        {
            //默认是不支持Get请求的,需要指定允许
            return Json(new { id=1,name="blank"},JsonRequestBehavior.AllowGet);
        }

向客户端输出状态码,状态码状态很多,可以根据实际情况返回

    public ActionResult GetCode()
        {
            return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound);
        }

//分部页面

        public ActionResult GetPartial()
        {
            //返回分部页面,类似一个Vue中的组件,可以在需要的地方重复使用
            return PartialView();
        }

可以在前端页面这样调用,类似使用组件

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

@Html.Action("GetPartial")

上面演示的带action处理的,还支持静态的页面

_Login.cshtml 分部页面内容

<input type="text"   name="name" value="" />
<input type="password"  name="pwd" value="" />
<button>登录</button>


@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

@* 调用Action页面 *@
@Html.Action("GetPartial")
@* 调用静态页面,支持传参到页面 *@
@Html.Partial("_Login",new ASP.NET_MVC基础_2.Models.Student())