ASP.NET MVC中Controller返回值类型ActionResult
1、返回ViewResult视图结果,将视图呈现给网页
public class TestController : Controller
{
//必须存在Controller\Test\Index.cshtml文件
public ActionResult Index()
{
return View();
}
}
2、 返回PartialViewResult部分视图结果,主要用于返回部分视图内容
//在View/Shared目录下创建ViewUserControl.cshtml部分视图
public ActionResult UserControl()
{
return PartialView("ViewUserControl");
}
3、 返回ContentResult用户定义的内容类型
public ActionResult Content()
{
return Content("Test Content", "text/html"); // 可以指定文本类型
}
4、 返回JsonResult序列化的Json对象
public ActionResult Index()
{
//主要用于返回json格式对象,可以用ajax操作;
//注意:需要设置参数,JsonRequestBehavior.AllowGet,
//否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
//若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("id", );
dic.Add("name", "hello");
return Json(dic, JsonRequestBehavior.AllowGet);
}
5、返回JavaScriptResult可在客户端执行的脚本
public ActionResult Index()
{
//但这里并不会直接响应弹出窗口,需要用页面进行再一次调用。
//这个可以方便根据不同逻辑执行不同的js操作
string str = string.Format("alter('{0}');", "弹出窗口");
return JavaScript(str);
}
6、返回FileResult要写入响应中的二进制输出,一般可以用作要简单下载的功能
public ActionResult Index()
{
//直接下载test.zip后保存到本地则为"文件显示名称.zip
string fileName = "~/Content/test.zip"; // 文件名
string downFileName = "文件显示名称.zip"; // 要在下载框显示的文件名
return File(fileName, "application/octet-stream", downFileName);
}
7.返回Null或者Void数据类型的EmptyResult
public ActionResult Index()
{
return null;
}
8、重定向方法:Redirect / RedirectToAction / RedirectToRoute
public class TestController : Controller
{
public ActionResult Index(int id = , string name = "")
{
return View();
} //Redirect:直接转到指定的url地址
public ActionResult Redirect()
{
return Redirect("http://www.baidu.com");
} //RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName
public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Test", new { id = , name = "liu" });
} //RedirectToRoute:指定路由(RouteConfig注册的路由规则)进行跳转
public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Test", action = "Index" });
}
}
ASP.NET MVC中Controller返回值类型ActionResult的更多相关文章
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- MVC 中Controller返回值类型ActionResult
下面列举Asp.net MVC中Controller中的ActionResult返回类型 1.返回ViewResult视图结果,将视图呈现给网页 public ActionResult About() ...
- dotNET开发之MVC中Controller返回值类型ActionResult方法总结
1.返回ViewResult视图结果,将视图呈现给网页 2. 返回PartialViewResult部分视图结果,主要用于返回部分视图内容 3. 返回ContentResult用户定义的内容类型 4. ...
- 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 ...
- ASP.NET MVC – 关于Action返回结果类型的事儿(上)
原文:ASP.NET MVC – 关于Action返回结果类型的事儿(上) 本文转自:博客园-文超的技术博客 一. ASP.NET MVC 1.0 Result 几何? Action的 ...
- Web API中的返回值类型
WebApi中的返回值类型大致可分为四种: Void/ IHttpActionResult/ HttpResponseMessage /自定义类型 一.Void void申明方法没有返回值,执行成功后 ...
随机推荐
- Laravel 5.1 文档攻略 —— Eloquent:模型关系
简介 其实大家都知道,数据表之间都是可以关联的,前面讲过了,Eloquent ORM是数据模型操作代替表操作,那么表的关联查询,在Eloquent这里也就是模型间的关联查询,这就是本章的主要内容: E ...
- 跟着百度学PHP[4]-OOP面对对象编程-3-实例化一个对象
当定义好类后,我们使用new关键字来实例化一个对象! 格式: $object = new 类名; <?php class Person{ private $name; "; priva ...
- const 和宏的区别
参考:http://blog.sina.com.cn/s/blog_79b01f6601018xdg.html (1) 编译器处理方式不同 define宏是在预处理阶段展开. const常量是编译运行 ...
- 31 GroupSock(AddressString)——live555源码阅读(四)网络
31 GroupSock(AddressString)——live555源码阅读(四)网络 31 GroupSock(AddressString)——live555源码阅读(四)网络 简介 Addre ...
- Pólya计数定理
我日啊..被cls的计数题虐得欲仙欲死...根本不会计数QAQ... 不懂数学啊... 前置技能 群 群是二元组\((G,*)\),满足 \(*:(G,G)\rightarrow G\) \(\exi ...
- CSS3 text-overflow 属性
1. <!DOCTYPE html> <html> <head> <style> div.test { white-space:nowrap; widt ...
- poj 2403
http://poj.org/problem?id=2403 题意:就是给你m个单词,以及n段对话.每一个单词都有所对应的价值.求对话中的价值总和 题解:很简单,就是用单词和价值对应起来,然后再寻找就 ...
- POJ 1321
http://poj.org/problem?id=1321 一道深搜的题目,和那个POJ3740有点相类似. 也是到了现在我才知道原来深搜也有几种套路的,以前我的都是用队列来做,那个是不需要记住什么 ...
- python之errno
http://www.cnblogs.com/Security-Darren/p/4168392.html errno.EWOULDBLOCK 操作将会阻塞
- poj4052
题意:求一个文章(长度5.1e6)里面出现了多少个指定的模式串.重复出现只记一次.而且如果两个模式串都出现的情况下,一个是另一个的子串,则该子串不算出现过. 分析:AC自动机. 由于子串不算所以加一些 ...