首页 技术 正文
技术 2022年11月20日
0 收藏 753 点赞 4,858 浏览 1816 个字

1、ActionResult

ActionResult是一个父类,

子类包括了我们熟知的

ViewResult 返回相应的视图

ContentResult  返回字符串

RedirectResult( return Redirect(url:xxxx))  重定向

RedirectToRouteResult (return RedirectToAction(actionName,controllerName:xxx))  根据路由重定向

FileResult 向客户端输出文件

JsonResult 向客户端返回JSON对象

HttpStatusCodeResult 显示不同的状态码

PartialViewResult 返回部分页面

跳转到其他的行为(方法)

返回一个文件

一个图片的上传与查看功能实现

 <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<img src="/Demo/BrowsePic?name=固定的name" alt="Alternate Text" />
<form action="/Demo/UploadFile" method="post" enctype="multipart/form-data">
<input type="file" name="pic" value="" />
<input type="submit" name="" value="提交" />
</form>
</body>
</html>
        public ActionResult UploadFile(HttpPostedFileBase pic)
{
var filename = DateTime.Now.Ticks + pic.FileName;
pic.SaveAs(filename: Request.MapPath(basePath + filename));
var info = filename + "has been saved.";
return Content(filename);
} public ActionResult BrowsePic(string name)
{
return File(basePath + name, contentType: "image/jpg");
}

获取文件大小

file.ContentLength 单位是Byte

2、JSON

默认不允许get请求

        public ActionResult Index()
{
// 允许get请求
return Json(new { id = , name = "Jack" }, JsonRequestBehavior.AllowGet);
}

单独写Action进行ajax操作

3、状态码

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

.InternalServerError 500

4、部分视图

不会添加Layout

        public PartialViewResult GetPartial()
{
return PartialView();
}
<h2>这是部分页面的视图</h2>

作用类似于组件,适合作为组件被使用,使用方法:

<h1>index page for demo</h1><p>引用部分视图</p>@Html.Action("GetPartial")

另外,也可以在shared文件夹下创建分部页:

但是使用方法与上面不同:

<h1>index page for demo</h1><p>引用部分视图</p>@Html.Action("GetPartial")
@Html.Partial("_PartialPage1")

这种方式如何传参?通过其第二个参数:

主视图:

@using MVCStudy.Models<h1>index page for demo</h1><p>引用部分视图</p>@Html.Action("GetPartial")
@Html.Partial("_PartialPage1", new Animal{ Name = "pig" ,Sex = "male"})

分部视图:

@model MVCStudy.Models.Animal
<h2>你好,我是在shared目录下创建的分部页</h2><p>接下来是我所接收的参数</p><p>@Model.Name</p>
<p>@Model.Sex</p>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899