Asp.net MVC 中Controller返回值类型ActionResult
【Asp.net MVC中Controller返回值类型】
在mvc中所有的controller类都必须使用"Controller"后缀来命名
并且对Action也有一定的要求:
- 必须是一个public方法
- 必须是实例方法
- 没有标志NonActionAttribute特性的(NoAction)
- 不能被重载
- 必须返回ActionResult类型
public class MyController : Controller
{
// 必须返回ActionResult类型
public ActionResult HelloWorld()
{
ViewData["Message"] = "Hello World!";
return View();
}
}下面列举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数据类型的EmptyResultpublic ActionResult Empty()
{
return null;
}返回NULL
8、重定向方法:Redirect / RedirectToAction / RedirectToRouteRedirect:直接转到指定的url地址
public ActionResult Redirect()
{
// 直接返回指定的url地址
return Redirect("http://www.baidu.com");
}RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName
public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Home", new { id = "", name = "liu" });
}也可以带上参数
RedirectToRoute:指定路由进行跳转public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Home", action = "Index"});
}Default为global.asax.cs中定义的路由名称
声明:本文转自blog.csdn.net ,作者pasic
Asp.net MVC 中Controller返回值类型ActionResult的更多相关文章
- ASP.NET MVC中Controller返回值类型ActionResult
1.返回ViewResult视图结果,将视图呈现给网页 public class TestController : Controller { //必须存在Controller\Test\Index.c ...
- 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申明方法没有返回值,执行成功后 ...
随机推荐
- leetcode 149. Max Points on a Line --------- java
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- 工作中遇到的问题--使用DTO减少数据字段
Location中包含如下字段以及AMfgObject中关于创建信息的字段,然而有时使用并不需要传输那么多数据,则对其中字段进行过滤. @Entity@Table(name = "LOCAT ...
- kuangbin_ShortPath K (POJ 3159)
很简单的模板题 放在K那么后的位置的原因大概是 光看题意并不是很容易想到是用最短路解吧 奈何kuangbin分在了最短路专题 一发水过 #include <iostream> #inclu ...
- Linux内核源代码的结构(转)
源代码所有在目录:/usr/src/linux (大部分linux发行版本中) init 内核初始化代码 kernel 内核核心部分:进程.定时.程序执行.信号.模块... mm 内存处理 a ...
- JQ插件jquery.fn.extend与jquery.extend
jQuery为开发插件提拱了两个方法,分别是: JavaScript代码 jQuery.fn.extend(object); jQuery.extend(object); jQuery.extend( ...
- 另一个分区工具GNU的parted[转自vbird]
利用 GNU 的 parted 进行分割行为 虽然你可以使用 fdisk 很快速的将你的分割槽切割妥当,不过 fdisk 却无法支持到高于 2TB 以上的分割槽! 此时就得需要 parted 来处理了 ...
- 使用iostat分析IO性能
对于I/O-bond类型的进程,我们经常用iostat工具查看进程IO请求下发的数量.系统处理IO请求的耗时,进而分析进程与操作系统的交互过程中IO方面是否存在瓶颈. 下面通过iostat命令使用实例 ...
- js调用百度地图API创建地图,搜索位置
实现代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <met ...
- Linux进程间通信-命名管道
前面我们讲了进程间通信的一种方式,匿名管道.我们知道,匿名管道只能用于父子关系的进程之间.那么没有这种关系的进程之间该如何进行数据传递呢? 1.什么是命名管道 匿名管道是在缓存中开辟的输出和输入文件流 ...
- IIS用户权限备忘
经常在网站部署到IIS遇到IIS帐户没有权限的问题,总是在看IIS Admin Service,但发现些帐户是有权限的. 其实针对相应的站点,应该看的是Application Pool的运行帐户,这个 ...