using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcAppTest1.Controllers
{
public class TestController : Controller
{
public class Customer
{
public string CustomerName { get; set; }
public string Address { get; set; }
public override string ToString()
{
return this.CustomerName+"|"+this.Address;
}
}
//
// GET: /Test/
public ActionResult Index()
{
return View();
}
public string GetString()
{
return "Hello World is old now. It’s time for wassup bro ;)";
}
public Customer GetCustomer()
{
Customer c = new Customer();
c.CustomerName = "Customer 1";
c.Address = "Address1";
return c;
}
[NonAction]
public string SimpleMethod()
{
return "Hi, I am not action method";
}
public ActionResult GetView()
{
return View("MyView2");
}
public ActionResult GetView2()
{
return View("MyView");
}
static int i = 0;
public ActionResult GetContent()
{
i = (++i) % 3;
ActionResult ret = null;
switch (i)
{
case 0:
ret = View("MyContent");
break;
case 1:
ret = View("MyView");
break;
case 2:
ret = View("MyView2");
break;
}
return ret;
}
}
}
5. ActionResult和 ViewResult的关系是什么?
ActionResult是抽象类,而ViewResult是ActionResult的多级孩子节点,多级是因为ViewResult是ViewResultBase的子类,而ViewResultBase是ActionResult的孩子节点。
6. 什么是ContentResult?
ViewResult是HTML响应而ContentResult是标准的文本响应,仅返回字符串类型。区别就在于ContentResult是ActionResult的子类。
经过了本节MVC基础知识的学习,相信大家对MVC已经有了基本的认识。大家在进行ASP.NET MVC的开发时,还可以借助一些开发工具。ComponentOne Studio ASP.NET MVC 是一款轻量级控件,与Visual Studio无缝集成,完全与MVC6和ASP.NET 5.0兼容,将大幅提高工作效率。
下一节,我们将讨论模型,验证,Jquery和Json。所以请持续关注吧!ComponentOne Studio!
原文链接:http://www.codeproject.com/Articles/866143/Learn-MVC-step-by-step-in-days-Day