Controller返回值类型ActionResult
在mvc中所有的controller类都必须使用"Controller"后缀来命名
并且对Action也有一定的要求:
- 必须是一个public方法
- 必须是实例方法
- 没有标志NonActionAttribute特性的(NoAction)
- 不能被重载
- 必须返回ActionResult类型
1、返回ViewResult视图结果,将视图呈现给网页
public ActionResult About()
{
return View(); // 参数可以返回model对象
}
public ActionResult UserControl()
{
ViewBag.Message = "部分视图";
return PartialView("ViewUserControl");
}
页面调用@ViewBag.Message 将输出“部分视图”
public ActionResult Content()
{
return Content("Test Content", "text/html"); // 可以指定文本类型
}
此类型多用于在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);
}
注意:需要设置参数,JsonRequestBehavior.AllowGet,
否则会提示错误:此请求已被阻止,因为当用在 GET 请求中时,会将敏感信息透漏给第三方网站。
若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet。
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);
}
7、 返回Null或者Void数据类型的EmptyResult
public ActionResult Empty()
{
return null;
}
返回NULL
8、重定向方法:Redirect / RedirectToAction / RedirectToRoute
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的更多相关文章
- Asp.net MVC 中Controller返回值类型ActionResult
[Asp.net MVC中Controller返回值类型] 在mvc中所有的controller类都必须使用"Controller"后缀来命名并且对Action也有一定的要求: 必 ...
- 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. ...
- Spring MVC controller返回值类型
SpringMVC controller返回值类型: 1 String return "user":将请求转发到user.jsp(forword) return "red ...
- SpringMVC Controller 返回值几种类型
SpringMVC Controller 返回值几种类型 2016年06月21日 19:31:14 为who而生 阅读数:4189 标签: Controller 返回值类型spring mvc 更多 ...
- Controller 中Action 返回值类型 及其 页面跳转的用法
•Controller 中Action 返回值类型 View – 返回 ViewResult,相当于返回一个View 页面. -------------------------------- ...
- SpringMVC Controller 返回值的可选类型
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...
- 转SpringMVC Controller 返回值的可选类型
spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...
随机推荐
- ASP.NET MVC3.0或4.0设置二级域名的方法
之前我就想做二级域名指向同一个IP同一个程序无非是在路由匹配规则上做文章也就是对Url的重写的一种思路.我用了半天时间上网查阅了相关资料并做了Demo测试是完全 以的,在这分享给大家... 假如网站主 ...
- Arcgis Server ecp(许可)
Arcgis Server 10 许可(新建.txt文件,然后拷贝好以下内容,然后修改为 .ecp文件即可): 3dengine,100,ecp.arcgis.server,none,KGE784S1 ...
- J2EE学习记录,EJB,JNDI,RMI
Java EE 是java平台企业版(Java Platform Enterprise Edition)缩写,是Sum公司为企业级应用推出的标准平台. 随着Java技术的发展,J2EE平台得到了迅速的 ...
- hihoCoder挑战赛14 A,B,C题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目1 : 不等式 时间限制:10000ms 单点时限:1000ms 内存限制:2 ...
- 翻译:《What can I hold you with?》—— 博尔赫斯《英文诗两首》之一。
What can I hold you with? 我拿什么才能留住你? I offer you lean streets, desperate sunsets, the moon of the ja ...
- mysql索引之唯一索引
mysql 的唯一索引一般用于不重复的字段,一般会把表中的id设为唯一索引,创建唯一索引的目的不是为了提高查询速度,而是为了避免数据重复,注意:唯一索引可以有多个,但是列值必须唯一,创建唯一索引使用关 ...
- Selinux 禁用
Selinux是对于强制访问控制的实现,在这种访问控制体系的限制下,进程只能访问那些在他的任务中所需要文件. 对于新手来说会影响我们的操作.一般情况下是不需要的,所以禁用他,如果需要的情况下,我们可以 ...
- FileInputStream
InputStream 基类,抽象类 FileInputStream 读取文件的字节流 BufferedInputStream 缓冲输入字符流 package file; import java.io ...
- SqlServer2008 新功能:简单数据加密
一.首先要把密码字段改成 varbinary 类型. CREATE TABLE [dbo].[UserInfo]( [id] [int] IDENTITY(1,1) NOT NULL, [name] ...
- MVC每层的职责
MVC模式把应用程序分割成三层:模型.视图和控制器. 模型:模型代表着核心的业务逻辑和数据.模型封装了域实体的属性和行为,并暴露出了实体的属性. 视图: 视图负责转换模型并把它传递给表示层.视图应关注 ...