码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET 控制器

时间:2017-07-11 15:55:30      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:rip   使用   att   匿名对象   tar   模型   phone   ++   edit   

1.继承Controller
 public class TestController : Controller


2.编写控制器方法
    // URL  :   test/Edit/1
        [HttpPost]//http请求方式
        public ActionResult Edit(int id, FormCollection collection)
        {
    //public (必须返回类型) 名称  (参数类型 参数,FormCollection:获取表单数据)  
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

3.返回值类型:

在mvc中所有的controller类都必须使用"Controller"后缀来命名 并且对Action也有一定的要求:

    必须是一个public方法
    必须是实例方法
    没有标志NonActionAttribute特性的(NoAction)
    不能被重载



Asp.net MVC中Controller返回值类型:

    ActionResult:返回视图,model对象()
    ViewResult:返回视图
    ContentResult:返回字符串(可以用于json 字符串)
    JsonResult:返回JsonResult类型(返回json)
    RedirectToRouteResult:控制器跳转控制器




参考资料:http://blog.csdn.net/pasic/article/details/7110134

 

 

 

ViewResult

返回值为ViewResult即返回展示给用户的前台页面(视图),return View()可以返回本控制器内的任意视图。当return View()返回与Action不同名的视图时会直接展示视图,而不是执行对应的Action方法。

public ViewResult Index()
{
    UserInfoModel userInfoModel = new UserInfoModel();
    userInfoModel.AddTime = DateTime.Now;
    userInfoModel.UserName = "用户名";
    int number = 123;
    //【重载1】默认返回与Action同名的视图
    return View();
    //【重载2】返回本控制器下的Add视图
    return View("Add");
    //【重载3】一般传递的参数对象的类型与视图的模型类一致
    return View("Add", userInfoModel);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

 

ContentResult

ContentResult用于将字符串直接向客户端输出。
后台代码:

public ContentResult GetUserInfo()
{
    string userInfo = "[{\"UserName\":\"蝈蝈\",\"Age\":\"18\",\"PhoneNumber\":\"18233199999\"}," +
                      "{\"UserName\":\"果果\",\"Age\":\"16\",\"PhoneNumber\":\"18233199999\"}," +
                      "{\"UserName\":\"郭郭\",\"Age\":\"20\",\"PhoneNumber\":\"18233199999\"}]";
    return Content(userInfo);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

 

前台代码:

function TestActionResult_Index_AddUserInfo1(userInfo) {
    //先把字符串转换为Json对象
    var userInfoArray = JSON.parse(userInfo);
    for (var i = 0; i < userInfoArray.length; i++) {
        $("<tr></tr>").append("<td>" + userInfoArray[i].UserName + "</td>")
                      .append("<td>" + userInfoArray[i].Age + "</td>")
                      .append("<td>" + userInfoArray[i].PhoneNumber + "</td>")
                      .appendTo($("#TestActionResult_Index_tb"));
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

 

JsonResult

JsonResult首先将指定的对象序列化为Json字符串,然后将字符串写入到HTTP输出流。由于Json字符串在MVC前后台交互过程中使用频率很高,所以多举几个例子。
1)返回Json数组

public JsonResult GetUserInfoJsonArray()
{
    List<object> userInfoList = new List<object>();
    //userInfoList添加匿名对象
    userInfoList.Add(new
    {
        UserName="蝈蝈",Age=18,PhoneNumber="18233199999"
    });
    userInfoList.Add(new
    {
        UserName="果果",Age=16,PhoneNumber="18233199999"
    });
    userInfoList.Add(new
    {
        UserName="郭郭",Age=20,PhoneNumber="18233199999"
    });
    //如果请求方式为get,则必须设置JsonRequestBehavior.AllowGet
    return Json(userInfoList, JsonRequestBehavior.AllowGet);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 1

2)返回Json对象

public JsonResult GetUserInfoJsonObject()
{
    List<object> userInfoList = new List<object>();
    userInfoList.Add(new
    {
        UserName = "蝈蝈",Age = 18,PhoneNumber = "18233199999"
    });
    userInfoList.Add(new
    {
        UserName = "果果",Age = 16,PhoneNumber = "18233199999"
    });
    userInfoList.Add(new
    {
        UserName = "郭郭",Age = 20,PhoneNumber = "18233199999"
    });
    //【方法1】返回匿名对象或实例对象
    var resultList = new
    {
        Company = "热血传奇",
        President = "蝈蝈",
        UserInfoList = userInfoList
    };
    return Json(resultList, JsonRequestBehavior.AllowGet);
    //【方法2】返回Dictionary对象
    Dictionary<string, object> dict = new Dictionary<string, object>()
    {
        {"Company","热血传奇"},
        {"President","蝈蝈"},
        {"UserInfoList",userInfoList}
    };
    return Json(dict, JsonRequestBehavior.AllowGet);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 2

RedirectToRouteResult

RedirectToRouteResult可以跳转到本项目内任意控制器的Action,当重定向路由时会根据参数执行对应的控制器下的Action方法,而不是直接展示页面。

public ViewResult Index()
{
    UserInfoModel userInfoModel = new UserInfoModel();
    userInfoModel.AddTime = DateTime.Now;
    userInfoModel.UserName = "用户名";
    int number = 123;            
    //【重载1】访问Home控制器下的Add方法        
    return RedirectToAction("Add", "Home");
    //【重载2】访问Home控制器下的Add方法,并且参数id=6
    return RedirectToAction("Add", "Home", new {id = 6});
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

 

ContentResult与JsonResult返回Json数据的区别

1.当返回到前端的json数据,不标准时如–”{\”Age\”:\”abcde\”,\”name\”:\”rer\”}”,前端抓包的值和我给的一样。不管你的返回值类型是
JsonResult还是String,都需要使用JSON.parse(Data)手动转换一下,才能把字符串变为JSON对象。
2.当返回到前端的json数据,标准时如–”{“Age”:”abcde”,”name”:”rer”}”,前端抓包的值和我给的一样。
只需要保证响应报文头的ContentType = “application/json,JQ 都会自动把JSON字符串转换为JSON对象。

ASP.NET 控制器

标签:rip   使用   att   匿名对象   tar   模型   phone   ++   edit   

原文地址:http://www.cnblogs.com/j2ee-web-01/p/7150665.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!