标签:
我们上边所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件。而它的返回类型是ActionResult如
public ActionResult Index()
{
return View();
}
除了View()之外那我们这里还能用于返回什么值呢?
场景:要返回代码片断,比如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("这是一段文本");
}
有时我们在调用Ajax时还会要求返回对象为Json序列化的结果,如:
public ActionResult ShowJson()
{
var m = new EiceIndexModel
{
Name = "邹健",
Sex = true
};
return Json(m);
}
返回文本:
{"Name":"邹健","Sex":true}
大多时候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"//文件类型
);
}
本文转自:http://blog.csdn.net/pasic/article/details/7110134
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", 100);
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 public ActionResult Redirect()
{
// 直接返回指定的url地址
return Redirect("http://www.baidu.com");
}
RedirectToAction:直接使用 Action Name 进行跳转,也可以加上ControllerName
public ActionResult RedirectResult()
{
return RedirectToAction("Index", "Home", new { id = "100", name = "liu" });
}
也可以带上参数RedirectToRoute:指定路由进行跳转
public ActionResult RedirectRouteResult()
{
return RedirectToRoute("Default", new { controller = "Home", action = "Index"});
}
Default为global.asax.cs中定义的路由名称标签:
原文地址:http://www.cnblogs.com/lizhenlin/p/5518267.html