请求什么?
请求一个服务,对于程序来讲就是请求一个或多个功能。对于C#来讲就是请求类中的一个方法
如何请求?
在B/S结构用户请求Server的唯一途径就是URL(统一资源定位语言,俗称网站)
类名和方法在URL上怎么体现?
http://localhost:8080/类名/方法名不是所有的类都能访问
类名必须以Controller结尾
必须继承Controller抽象类
类必须是public的
专业术语:这样的类叫控制器
必须继承Controller抽象类
类必须是public的
专业术语:这样的类叫控制器
//控制器代码示例:
namespace Study.Controllers
{
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
}
}//第一次新建一个Cotroller运行时,需要重新生成解决方案
不是所有的方法都能访问
方法必须是public
方法不能加特性[NonAction]
方法不能重载
重载的方法需要通过特性区分
方法不能加特性[NonAction]
方法不能重载
重载的方法需要通过特性区分
特性:
[HttpGet][HttpPost]
[NoAction]
[ActionName("reg")] //取别名
专业术语:这样的方法叫Action
//取别名访问:
public class TestController : Controller
{
public ActionResult Index()
{
return View();
}
[ActionName("World")] //此处可更改为[HttpGet],通过请求方式访问
public ActionResult test()
{
return Content("Hello World");
}
[ActionName("city")] //此处可更改为[HttpPost],通过请求方式访问
public ActionResult test(String a)
{
return Content("Hello city");
}
}
控制器默认代码解析
View方法是什么?怎么用?
// 8种重载
View(); //返回与Action名称相同的视图文件
View(object model); //文件查找同上,将Model传递到页面
View(IView view); //呈现自定义的视图
View(string viewName); //呈现指定的视图文件
View(IView view, object model); //在自定义视图传递Model对象
View(string viewName, object model); //在指定的页面传递Model对象
View(string viewName, string masterName); //在视图页面使用母版页,第二个参数为 母版页的路径
View(string viewName, string masterName, object model); //在使用母版页的视图中传递Model对象
ActionResult是什么?
//ActionResult是一个抽象类
//ActionResult的子类
return Content("");
return new EmptyResult();
return new HttpUnauthorizedResult();
return JavaScript("<script></script>");
return Json();
return Redirect("url");
return RedirectToAction("");
return RedirectToRoute("");
Action方法必须返回ActionResult类型吗?
可以返回任何有效的数据类型