MVC Controller return 格式
所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件。而它的返回类型是ActionResult如
public ActionResult Index()
{
return View();
}
除了View()之外那我们这里还能用于返回什么值呢?
一、ascx页面
场景:要返回代码片断,比如Ajax返回一个子页
我们先新建一个Action
public ActionResult Ascx()
{
return PartialView();
}
我们下面再建一个View,仍然是在Action中点右键,AddView。
注意图中勾选。
于是新建了一个ascx页,我们将之少做改写一下
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <div> 得到一个DIV </div>
运行,得到页面
二、返回文本
除了上述情况,有时我们还会仅返回一段文本。
此时我们可以使用以下Action形式:
public ActionResult Text()
{
return Content("这是一段文本");
}
三、返回Json
有时我们在调用Ajax时还会要求返回对象为Json序列化的结果,如:
public ActionResult ShowJson()
{
var m = new EiceIndexModel{Name = "邹健",Sex = true};
return Json(m);
}
返回文本:
{"Name":"邹健","Sex":true}
四、输出JS文件
大多时候js文件都是静态的,但有时js文件可能也要动态生成这时我们可以这样输出
public ActionResult Js()
{
return JavaScript("var x=0;");
}
我们访问之,得到一个正常页面但其Content-Type:application/x-javascript; charset=utf-8
五、页面跳转
1.跳转到Url
public ActionResult rdurl()
{
return Redirect("http://www.baidu.com");
}
2.跳转到Action
public ActionResult rdaction()
{
return RedirectToAction("Index","Eice");
}
3.跳转到Routing规则
public ActionResult rdrouting()
{
return RedirectToRoute("Default",//Route名
new{Controller = "Eice",Action = "Index" });
}
六、显示文件
public ActionResult fn()
{
return File("/Content/site.css"//文件路径
, "text/css"//文件类型
);
}
MVC Controller return 格式的更多相关文章
- MVC Controller return 格式分类及用法
概述 所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件.而它的返回类型是ActionResult如 public ActionResult Index ...
- MVC Controller return 格式之JsonResult、ContentResult、RedirectResult……
//语法 public class JsonResult : ActionResult public class ContentResult : ActionResult public class ...
- mvc Controller类介绍
1.Controller类 i.Controller必须为公开类: ii.必须以Controller结尾: iii.继承Controller基类或实现IController接口的类: iv.类中必须包 ...
- .NET/ASP.NET MVC Controller 控制器(IController控制器的创建过程)
阅读目录: 1.开篇介绍 2.ASP.NETMVC IControllerFactory 控制器工厂接口 3.ASP.NETMVC DefaultControllerFactory 默认控制器工厂 4 ...
- MVC Controller Dependency Injection for Beginners【翻译】
在codeproject看到一篇文章,群里的一个朋友要帮忙我翻译一下顺便贴出来,这篇文章适合新手,也算是对MEF的一个简单用法的介绍. Introduction In a simple stateme ...
- 转:【Spring MVC Controller单例陷阱】
http://lavasoft.blog.51cto.com/62575/1394669/ Spring MVC Controller默认是单例的: 单例的原因有二:1.为了性能.2.不需要多例. 1 ...
- MVC Controller 链接到 API Controller 以及反向链接
MVC Controller 链接到 API Controller 以及反向链接 问题 想创建一个从 ASP.NET MVC controller 到 ASP.NET Web API controll ...
- Spring MVC Controller单例陷阱
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://lavasoft.blog.51cto.com/62575/1394669 Spr ...
- .net Mvc Controller 接收 Json/post方式 数组 字典 类型 复杂对象
原文地址:http://www.cnblogs.com/fannyatg/archive/2012/04/16/2451611.html ------------------------------- ...
随机推荐
- STM32的GPIO
一.I/O端口位的基本结构 二.端口位配置表 参考:STM32芯片参考手册
- u-boot代码学习内容
前言 u-boot代码庞大,不可能全部细读,只能有选择的读部分代码.在读代码之前,根据韦东山教材,关于代码学习内容和深度做以下预先划定. 一.Makefile.mkconfig.config.mk等 ...
- SDWebImage 详解
一.SDWebImage介绍 1.在项目的开发过程中,我们经常会用到异步加载图片的功能,先从网络上异步下载图片,然后通过UIImageView显示在屏幕上.这是一个经常使用的功能,基本上所有的联网应用 ...
- UM分享 - 详解
官网: http://dev.umeng.com 友盟现在发展的很壮大! 有熟为人知的社会化分享\统计分析\消息推送\即时通信\自动更新\多媒体服务等功能, 今天就其中第一项 分享功能, 做出分解. ...
- 数据采集服务提供商,ip提供商 里面有些不错的基础数据
http://user.qzone.qq.com/1649677458 这家公司的爬虫应该挺牛的 !@#!#!~#¥¥¥@@http://www.site-digger.com/
- SCU3502 The Almost Lucky Number
Description A lucky number is a number whose decimal representation contains only the digits \(4\) a ...
- Swift与Objective-C中的闭包
Swift Code: func makeIncrementor(forIncrement amount: Int) -> (() -> Int,() -> Int) { func ...
- 一个简单的DDraw应用程序
阅读排行榜 1. C/C++ 笔试.面试题目大汇总(72915) 2. [STL]list基础(21718) 3. COM笔记-CoCreateInstance(14842) 4. C/C++ ...
- 剖析Qt的事件机制原理
版权声明 请尊重原创作品.转载请保持文章完整性,并以超链接形式注明原始作者“tingsking18”和主站点地址,方便其他朋友提问和指正. QT源码解析(一) QT创建窗口程序.消息循环和WinMai ...
- 【HDOJ】1166 敌兵布阵
线段树. #include <stdio.h> #define maxn 55555 ]; void PushUP(int rt) { sums[rt] = sums[rt<< ...