6.1Controller类的基本构成

关于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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值