关于Controller
Controller是什么?
Controller的历史
定义一个Controller
使用IController接口
Public interface Icontroller{
void Execute(RequestContextrequestcontext);
}
using System.Web.Mvc;
using System.Web.Routing;
public class SimpleController: IController{
public void Execute(RequestContextrequestContext){
var
response = requestContext.HttpContext.Response;
response.Write(“<h1>Hello World!</h1>”);
}}
名字后面一定要叫Controller,不然Route不认识。
Controller的基础类实现
-IControllerBase接口
Controller类和Action类
System.Web.Controller
using System;using System.Web;using System.Web.Mvc;public class SimpleController: Controller{public void Hello(){response.Write(“<h1>Hello World!</h1>”);}}public void Goodbye(string name)
{
Response.Write(“Goodbye” + HttpUtility.HtmlEncode(name));
}
url:/simple2/goodbye?name=World
url:/simple2/goodbye/world
使用多个参数
public void Distance(int x1, int y1, int x2, int y2)
{
double xSquared= Math.Pow(x2 -x1, 2);
double ySquared= Math.Pow(y2 -y1, 2);
Response.Write(Math.Sqrt(xSquared+ ySquared));
}
/simple2/distance?x2=1&y2=2&x1=0&y1=0
routes.MapRoute(“distance”,“simple2/distance/{x1},{y1}/{x2},{y2}”,new { Controller = “Simple2”, action = “Distance” });
/simple2/distance/0,0/1,2
默认URL
public ActionResultDinnersNearMe(string location, intmaxDinners= 10)
{}
2011-4-17 16:00 danny
Controller是什么?
Controller的历史
定义一个Controller
使用IController接口
Public interface Icontroller{
void Execute(RequestContextrequestcontext);
}
using System.Web.Mvc;
using System.Web.Routing;
public class SimpleController: IController{
public void Execute(RequestContextrequestContext){
var
response = requestContext.HttpContext.Response;
response.Write(“<h1>Hello World!</h1>”);
}}
名字后面一定要叫Controller,不然Route不认识。
Controller的基础类实现
-IControllerBase接口
Controller类和Action类
System.Web.Controller
using System;using System.Web;using System.Web.Mvc;public class SimpleController: Controller{public void Hello(){response.Write(“<h1>Hello World!</h1>”);}}public void Goodbye(string name)
{
Response.Write(“Goodbye” + HttpUtility.HtmlEncode(name));
}
url:/simple2/goodbye?name=World
url:/simple2/goodbye/world
使用多个参数
public void Distance(int x1, int y1, int x2, int y2)
{
double xSquared= Math.Pow(x2 -x1, 2);
double ySquared= Math.Pow(y2 -y1, 2);
Response.Write(Math.Sqrt(xSquared+ ySquared));
}
/simple2/distance?x2=1&y2=2&x1=0&y1=0
routes.MapRoute(“distance”,“simple2/distance/{x1},{y1}/{x2},{y2}”,new { Controller = “Simple2”, action = “Distance” });
/simple2/distance/0,0/1,2
默认URL
public ActionResultDinnersNearMe(string location, intmaxDinners= 10)
{}
2011-4-17 16:00 danny