MVC 中Controller返回值类型ActionResult
下面列举Asp.net MVC中Controller中的ActionResult返回类型
1、返回ViewResult视图结果,将视图呈现给网页
public ActionResult About()
{
return View(); // 参数可以返回model对象
}
2、 返回PartialViewResult部分视图结果,主要用于返回部分视图内容
在View/Shared目录下创建ViewUserControl.cshtml部分视图
public ActionResult UserControl()
{
ViewBag.Message = "部分视图";
return PartialView("ViewUserControl");
}
页面调用@ViewBag.Message 将输出“部分视图”
3、 返回ContentResult用户定义的内容类型
public ActionResult Content()
{
return Content("Test Content", "text/html"); // 可以指定文本类型
}
页面输出“Test Content”;
此类型多用于在ajax操作中需要返回的文本内容
4、 返回JsonResult序列化的Json对象

public ActionResult Json()
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("id", 100);
dic.Add("name", "hello");
return Json(dic, JsonRequestBehavior.AllowGet);
}

主要用于返回json格式对象,可以用ajax操作;
注意:需要设置参数,JsonRequestBehavior.AllowGet,
否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
5、返回JavaScriptResult可在客户端执行的脚本
public ActionResult JavaScript()
{
string str = string.Format("alter('{0}');", "弹出窗口");
return JavaScript(str);
}
但这里并不会直接响应弹出窗口,需要用页面进行再一次调用。
这个可以方便根据不同逻辑执行不同的js操作
6、返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能

public ActionResult File()
{
string fileName = "~/Content/test.zip"; // 文件名
string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
return File(fileName, "application/octet-stream", downFileName);
}

直接下载test.zip后保存到本地则为"文件显示名称.zip"
7、 返回Null或者Void数据类型的EmptyResult
public ActionResult Empty()
{
return null;
}
返回NULL
8、重定向方法:Redirect / RedirectToAction / RedirectToRoute
Redirect:直接转到指定的url地址
public ActionResult Redirect()
{
// 直接返回指定的url地址
return Redirect("http://www.baidu.com");
}
RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName,也可以带上参数
public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });
}
RedirectToRoute:指定路由进行跳转
public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Home", action = "Index"});
}
Default为global.asax.cs中定义的路由名称
内容转自:http://blog.csdn.net/pasic/article/details/7110134
MVC 中Controller返回值类型ActionResult的更多相关文章
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- dotNET开发之MVC中Controller返回值类型ActionResult方法总结
1.返回ViewResult视图结果,将视图呈现给网页 2. 返回PartialViewResult部分视图结果,主要用于返回部分视图内容 3. 返回ContentResult用户定义的内容类型 4. ...
- ASP.NET MVC中Controller返回值类型ActionResult
1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...
- Controller返回值类型ActionResult
在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...
- Spring MVC中Controller返回值void时报错
Controller如下: 当使用url访问该处理器方法时,报错如下: 26-Jan-2019 21:16:28.105 警告 [http-nio-8080-exec-39] org.springfr ...
- Controller 中Action 返回值类型 及其 页面跳转的用法
•Controller 中Action 返回值类型 View – 返回 ViewResult,相当于返回一个View 页面. -------------------------------- ...
- Spring MVC controller返回值类型
SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...
- Web API中的返回值类型
WebApi中的返回值类型大致可分为四种: Void/ IHttpActionResult/ HttpResponseMessage /自定义类型 一.Void void申明方法没有返回值,执行成功后 ...
- MVC方法的返回值类型
MVC方法返回值类型 ModelAndView返回值类型: 1.当返回为null时,页面不跳转. 2.当返回值没有指定视图名时,默认使用请求名作为视图名进行跳转. 3.当返回值指定了视图名,程序会按照 ...
随机推荐
- solr学习(六):使用自定义int/long类型主键
需求分析: 我不想使用solr默认的主键id,我想换成其他的,比如我的文章id为article_id,我想让article_id作为主键. 而且,我的主键是int类型,而solr的主键默认是strin ...
- SDOI2018IIIDX
/* 题目转换为 n个节点的一片森林,n个权值,要给每个节点分配一个权值,保证子节点的权值不小于父节点的权值,并且1~n的权值的字典序最大. 考场上的贪心很显然 建立出 树来 将所有数值从大到小排序 ...
- Java并发编程中的相关注解
引自:http://www.cnblogs.com/phoebus0501/archive/2011/02/21/1960077.html Java并发编程中,用到了一些专门为并发编程准备的 Anno ...
- day9笔记整理,记忆
函数的使用:一 定义函数的三种形式 1.1 无参函数 1.2 有参函数 1.3 空函数二 调用函数的三种形式 2.1 语句形式 def foo(): print('from foo') ...
- CentOS 开发环境准备
由于公司开发都是用的CentOS,如果不是使用docker的时候,难免会需要自己安装各种软件(例如,Python,nodejs等),然后这些软件还需要大量的依赖软件. 例如gcc等. 我们不需要一个一 ...
- springboot打包
springboot项目运行package命令,默认打出来的jar包只有几kb.想要打出可执行的jar包,加入插件: <build> <plugins> <plugin& ...
- python学习笔记目录
人生苦短,我学python学习笔记目录: week1 python入门week2 python基础week3 python进阶week4 python模块week5 python高阶week6 数据结 ...
- bootstrap-datepicker实现日期input readonly 标签中选择时间功能
引用datepicker css,js,zh-CH文件 ps: 都是基于bootstrap,所以得先引入bootstrap文件才可以使用 <link href="https://cdn ...
- Unity3D Update() 和 FixedUpdate()区别
Unity3D中 Update()与FixedUpdate()的区别是什么呢?从字面上理解,它们都是在更新时会被调用,并且会循环的调用.但是Update会在每次渲 染新的一帧时,被调用.而FixedU ...
- Eclipse properties.config.yml 配置文件中文编码问题。Eclipse 配置文件插件解决方案
写了中文默认转成unicode. 正常应该是这样子的 其实不是什么大问题只需要装一个插件就行了,插件有很多.推荐使用,Properties Editor 安装方式如下 .这里使用的是离线安装.即本地文 ...