我是靠谱客的博主 儒雅糖豆,这篇文章主要介绍ASP.NET MVC ActionResult的其它返回值,现在分享给大家,希望可以做个参考。

一、ascx页面

场景:要返回代码片断,比如Ajax返回一个子页

我们先新建一个Action

复制代码
1
2
3
4
public ActionResult Ascx() { return PartialView(); }

我们下面再建一个View,仍然是在Action中点右键,AddView。

image 注意图中勾选。

于是新建了一个ascx页,我们将之少做改写一下

复制代码
1
2
3
4
5
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <div> 得到一个DIV </div>

运行,得到页面

image 

二、返回文本

除了上述情况,有时我们还会仅返回一段文本。

此时我们可以使用以下Action形式:

复制代码
1
2
3
public ActionResult Text(){ return Content("这是一段文本"); }

三、返回Json

有时我们在调用Ajax时还会要求返回对象为Json序列化的结果,如:

复制代码
1
2
3
4
5
6
7
8
9
public ActionResult ShowJson() { var m = new EiceIndexModel { Name = "邹健", Sex = true }; return Json(m); }

返回文本:

复制代码
1
{"Name":"邹健","Sex":true}

四、输出JS文件

大多时候js文件都是静态的,但有时js文件可能也要动态生成这时我们可以这样输出

复制代码
1
2
3
4
public ActionResult Js() { return JavaScript("var x=0;"); }

我们访问之,得到一个正常页面但其Content-Type:application/x-javascript; charset=utf-8

五、页面跳转

1.跳转到Url

复制代码
1
2
3
4
public ActionResult rdurl() { return Redirect("http://www.baidu.com"); }

2.跳转到Action

复制代码
1
2
3
4
public ActionResult rdaction() { return RedirectToAction("Index","Eice"); }

3.跳转到Routing规则

复制代码
1
2
3
4
5
6
7
8
public ActionResult rdrouting() { return RedirectToRoute("Default",//Route名 new{ Controller = "Eice", Action = "Index" }); }

六、显示文件

复制代码
1
2
3
4
5
6
7
public ActionResult fn() { return File( "/Content/site.css"//文件路径 , "text/css"//文件类型 ); }

最后

以上就是儒雅糖豆最近收集整理的关于ASP.NET MVC ActionResult的其它返回值的全部内容,更多相关ASP.NET内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(85)

评论列表共有 0 条评论

立即
投稿
返回
顶部