下面列举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的更多相关文章

  1. Asp.net MVC 中Controller返回值类型ActionResult

    [Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...

  2. dotNET开发之MVC中Controller返回值类型ActionResult方法总结

    1.返回ViewResult视图结果,将视图呈现给网页 2. 返回PartialViewResult部分视图结果,主要用于返回部分视图内容 3. 返回ContentResult用户定义的内容类型 4. ...

  3. ASP.NET MVC中Controller返回值类型ActionResult

    1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...

  4. Controller返回值类型ActionResult

    在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求: 必须是一个public方法 必须是实例方法 没有标志NonA ...

  5. Spring MVC中Controller返回值void时报错

    Controller如下: 当使用url访问该处理器方法时,报错如下: 26-Jan-2019 21:16:28.105 警告 [http-nio-8080-exec-39] org.springfr ...

  6. Controller 中Action 返回值类型 及其 页面跳转的用法

        •Controller 中Action 返回值类型 View – 返回  ViewResult,相当于返回一个View 页面. -------------------------------- ...

  7. Spring MVC controller返回值类型

    SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...

  8. Web API中的返回值类型

    WebApi中的返回值类型大致可分为四种: Void/ IHttpActionResult/ HttpResponseMessage /自定义类型 一.Void void申明方法没有返回值,执行成功后 ...

  9. MVC方法的返回值类型

    MVC方法返回值类型 ModelAndView返回值类型: 1.当返回为null时,页面不跳转. 2.当返回值没有指定视图名时,默认使用请求名作为视图名进行跳转. 3.当返回值指定了视图名,程序会按照 ...

随机推荐

  1. jsfiddle将demo设置为public公开的

    jsfiddle的demo虽然可以通过链接分享给所有人,但是进入个人主页是没有的,需要将项目设置为公开public的 根据提示,打开demo项目页==>左侧菜单==>填写标题和描述==&g ...

  2. (转)CSS3:nth-child()伪类选择器,奇偶数行自定义样式first-child

    原文地址 Table表格奇偶数行定义样式: CSS3的强大,让人惊叹,人们在惊喜之余,又不得不为其艰难的道路感到可惜:好的标准只有得到行业浏览器的良好支持才算得上“标准”.CSS3标 准已提出数年,但 ...

  3. 【Selenium-WebDriver自学】Selenium-IDE调试(四)

    ==================================================================================================== ...

  4. android开发 写一个自定义形状的按键

    步骤: 1.在drawable 文件夹中创建一个xml布局文件. 2.修改布局文件 3.在需要使用背景的按键中导入布局. 创建布局文件: 修改布局文件: <?xml version=" ...

  5. [UGUI]渲染层级关系

    参考链接: http://blog.csdn.net/meegomeego/article/details/42060389 Unity中的渲染顺序自上而下大致可以分为三层: 1.Camera层.可以 ...

  6. springBoot_freemark配置

    Spring Boot –test 1.添加依赖 <!-- 引入 spring-boot-starter-test 集成单元测试--> <dependency> <gro ...

  7. 关于lampp中的proftpd的一些使用

    这个是配置文件 ServerName "ProFTPD" ServerType standalone DefaultServer on Port 21 这个是端口 Umask Ma ...

  8. 46.纯 CSS 创作一个在容器中反弹的小球

    原文地址:https://segmentfault.com/a/1190000015221260 练习地址:https://scrimba.com/c/c3GEWmTb 感想: 原来animation ...

  9. spark核心原理

    spark运行结构图如下: spark基本概念 应用程序(application):用户编写的spark应用程序,包含驱动程序(Driver)和分布在集群中多个节点上运行的Executor代码,在执行 ...

  10. 高程三 BOM 读书笔记

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...