ASP.NET MVC 第五回 ActionResult的其它返回值

摘要:
我们上边所看到的Action都是returnView();我们可以看作这个返回值用于解析一个aspx文件。
我们上边所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件。而它的返回类型是ActionResult如
      public ActionResult Index()
{
return View();
}
除了View()之外那我们这里还能用于返回什么值呢?

一、ascx页面

场景:要返回代码片断,比如Ajax返回一个子页
我们先新建一个Action
        public ActionResult Ascx()
{
return PartialView();
}
我们下面再建一个View,仍然是在Action中点右键,AddView。
image注意图中勾选。
于是新建了一个ascx页,我们将之少做改写一下
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>
<div>
得到一个DIV
</div>
运行,得到页面
image

二、返回文本

除了上述情况,有时我们还会仅返回一段文本。
此时我们可以使用以下Action形式:
        public ActionResult Text(){
return Content("这是一段文本");
}

三、返回Json

有时我们在调用Ajax时还会要求返回对象为Json序列化的结果,如:
        public ActionResult ShowJson()
{
var m = new EiceIndexModel
{
Name = "邹健",
Sex = true
};
return Json(m);
}
返回文本:
{"Name":"邹健","Sex":true}

四、输出JS文件

大多时候js文件都是静态的,但有时js文件可能也要动态生成这时我们可以这样输出
        public ActionResult Js()
{
return JavaScript("var x=0;");
}
我们访问之,得到一个正常页面但其Content-Type:application/x-javascript; charset=utf-8

五、页面跳转

1.跳转到Url
        public ActionResult rdurl()
{
return Redirect("[url]http://www.baidu.com[/url]");
}
2.跳转到Action
        public ActionResult rdaction()
{
return RedirectToAction("Index","Eice");
}
3.跳转到Routing规则
        public ActionResult rdrouting()
{
return RedirectToRoute("Default",//Route名
new{
Controller = "Eice",
Action = "Index"
});
}

六、显示文件

        public ActionResult fn()
{
return File(
"/Content/site.css"//文件路径
, "text/css"//文件类型
);
}
我们下一节讲过滤器Filter。

免责声明:文章转载自《ASP.NET MVC 第五回 ActionResult的其它返回值》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇sqlite3 语法C# 之 OpenFileDialog的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Linux 视频设备驱动V4L2最常用的控制命令使用说明

Linux视频设备驱动常用控制命令使用说明设置视频设备属性通过ioctl来进行设置,ioctl有三个参数,分别是fd, cmd,和parameter,表示设备描述符,控制命令和控制命令参数。1. 控制命令VIDIOC_QUERYCAP功能: 查询设备驱动的功能 ;参数说明:参数类型为V4L2的能力描述类型struct v4l2_capability;str...

tfgan折腾笔记(二):核心函数详述——gan_model族

定义model的函数有: 1.gan_model 函数原型: defgan_model( #Lambdas defining models. generator_fn, discriminator_fn, #Real data and conditioning. real_data, generator_inputs,...

解决 XMLHttpRequest status = 0 问题 及 返回值为null问题

1.XMLHttpRequest status = 0 问题 XMLHttpRequest的说明:http://www.w3.org/TR/XMLHttpRequest/ 。 The status attribute must return the result of running these steps: status的值一定会返回运行这些步骤的结果。...

python 函数返回值(总结)

  关键字:return   没有返回值的叫过程 def test1(): msg="我是一个过程" print(msg) 有return的叫函数 def test02(): msg="我是函数,有返回值" print(msg) return msg 关于返回的值: 定义的函数可以返回多个值,组合成元组def test03()...

VC:CString用法整理(转载)

1.CString::IsEmpty BOOL IsEmpty( ) const; 返回值:如果CString 对象的长度为0,则返回非零值;否则返回0。 说明:此成员函数用来测试一个CString 对象是否是空的。 示例: 下面的例子说明了如何使用CString::IsEmpty。 // CString::IsEmpty 示例 CStrin...

python-re正则、jsonpath返回值提取

re """ re.match 从头开始匹配 re.match(pattern, string, flags=0) 只匹配第一个,返回对象 先判断赋值的变量,加.group()返回值 re.search 匹配包含 re.search(pattern, string, flags=0) 只匹配一个,返回对象...