在mvc中所有的controller类都必须使用"Controller"后缀来命名

并且对Action也有一定的要求:

  • 必须是一个public方法
  • 必须是实例方法
  • 没有标志NonActionAttribute特性的(NoAction)
  • 不能被重载
  • 必须返回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", );
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");
}

9. RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName

public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Home", new { id = "", name = "liu" });
}
也可以带上参数
RedirectToRoute:指定路由进行跳转

Default为global.asax.cs中定义的路由名称

 

Controller返回值类型ActionResult的更多相关文章

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

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

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

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

  3. MVC 中Controller返回值类型ActionResult

    下面列举Asp.net MVC中Controller中的ActionResult返回类型 1.返回ViewResult视图结果,将视图呈现给网页 public ActionResult About() ...

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

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

  5. Spring MVC controller返回值类型

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

  6. SpringMVC Controller 返回值几种类型

    SpringMVC Controller 返回值几种类型 2016年06月21日 19:31:14 为who而生 阅读数:4189 标签: Controller 返回值类型spring mvc 更多 ...

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

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

  8. SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...

  9. 转SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...

随机推荐

  1. python challenge 待续中

    网址:http://www.pythonchallenge.com/解答好文:http://story.iteye.com/blog/730466 0:2^38 reduce(lambda x,y:x ...

  2. js获取页面名称

    function pageName() {    var strUrl = location.href;    var arrUrl = strUrl.split("/");    ...

  3. 最全C语言笔记回顾

  4. .Net HttpClient 模拟登录微信公众平台发送消息

    1.模拟登录 public WeiXinRetInfo ExecLogin(string name, string pass) { CookieContainer cc = new CookieCon ...

  5. xcode下载方式

    1.去AppStore下载 对于Xcode老是在AppStore升级失败,而且下载慢,可取找到了这个--> 官方 Xcode .dmg 文件下载链接:超级传送门 2.开发者中心官网下载 可参考这 ...

  6. 树形dp hdu1561

    有的堡垒攻克需要攻克另一个堡垒,形成一个森林,最多攻克m个堡垒,求获得宝物的最大价值. 1,以0做根将森林形成树: 2,用背包计算当前节点下需要攻克k个堡垒能获得的宝物最大价值,但是注意同一个根节点的 ...

  7. hdu5362 Just A String(dp)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Just A String Time Limit: 2000/1000 MS (J ...

  8. jQuery自学笔记(五):关于jQuery的遍历

    向上遍历 DOM 树 parent()  //返回被选元素的直接父元素,该方法只会向上一级对 DOM 树进行遍历. parents()    //返回被选元素的所有祖先元素,它一路向上直到文档的根元素 ...

  9. JS中setAttribute的兼容性问题(摘自leejersey)

    class和className兼容方法: object.setAttribute("class","content") 在IE8.Chrome.火狐.Opera ...

  10. JavaScript 系列笔记(一)数据类型

    关于JS的数据类型 简单类型有五种:Undifined, Null, Boolean, Number, String 复杂类型有一种:Object 通过typeof 操作符来获取数据类型,此操作符返回 ...