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

推荐订阅源

量子位
小众软件
小众软件
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
C
Check Point Blog
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
罗磊的独立博客
有赞技术团队
有赞技术团队
V
V2EX
Y
Y Combinator Blog
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
F
Fortinet All Blogs
W
WeLiveSecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Stack Overflow Blog
Stack Overflow Blog
The Cloudflare Blog
S
Security @ Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
MyScale Blog
MyScale Blog
Hugging Face - Blog
Hugging Face - Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
www.infosecurity-magazine.com
www.infosecurity-magazine.com
PCI Perspectives
PCI Perspectives
H
Heimdal Security Blog
Schneier on Security
Schneier on Security
Security Latest
Security Latest
AWS News Blog
AWS News Blog
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
博客园 - Franky
Cisco Talos Blog
Cisco Talos Blog
T
Threat Research - Cisco Blogs
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
N
News and Events Feed by Topic
Cloudbric
Cloudbric
Scott Helme
Scott Helme
云风的 BLOG
云风的 BLOG
Attack and Defense Labs
Attack and Defense Labs

博客园 - 取经路上

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())