<strong>一. 控制器介紹</strong>
<strong>1.1 使用IController創(chuàng)建Controller</strong>
控制器類必須實現(xiàn)IController接口。
public class BasicController : IController
{
public void Execute(RequestContext requestContext)
{
string controller = (string)requestContext.RouteData.Values["controller"];
string action = (string)requestContext.RouteData.Values["action"];
requestContext.HttpContext.Response.Write(string.Format("Controller:{0}, Action:{1}", controller, action));
}
}
<strong>1.2 創(chuàng)建派生與Controller類的控制器</strong>
Controller類提供了三個關(guān)鍵特性:
1.Action Method:不是只有一個Execute()方法,而是分解成多個方法。
2.Action Result:返回一個描述Action結(jié)果的對象。
3.Filter
<strong>1.3 接收請求數(shù)據(jù)</strong>
訪問來自請求的數(shù)據(jù)共三種方法:
1.Context上下文對象
2.參數(shù)
3.Model Binding
<strong>1.3.1 通過Context獲取數(shù)據(jù)</strong>


public ActionResult RenameProduct()
{
string userName = User.Identity.Name;
string serverName = Server.MachineName;
string clientIP = Request.UserHostAddress;
DateTime dateStamp = HttpContext.Timestamp;
string oldProductName = Request.Form["OldName"];
string newProductName = Request.Form["NewName"];
return View();
}
<strong>1.3.2 使用Action參數(shù)</strong>
對于實現(xiàn)IController接口的Controller:
public ActionResult ShowWeatherForecast(string city, DateTime forDate){}
<strong>1.4 產(chǎn)生輸出</strong>
public class BasicController : IController
{
public void Execute(RequestContext requestContext)
{
string controller = (string)requestContext.RouteData.Values["controller"];
string action = (string)requestContext.RouteData.Values["action"];
if (action.ToLower() == "redirect")
{
requestContext.HttpContext.Response.Redirect("/Derived/Index");
}
else
{
requestContext.HttpContext.Response.Write(string.Format("Controller:{0}, Action:{1}", controller, action));
}
}
}
對于派生與Controller類的控制器:
public void ProduceOutput()
{
if (Server.MachineName == "我的機器")
{
Response.Redirect("/Basic/Index");
} else
{
Response.Write("Controller: Derived, Action: ProduceOutput");
}
}
這些方法理論上可行,但是存在很多問題,如必須包含詳細的HTML和URL結(jié)構(gòu)等。
MVC框架提供了ActionResult解決這些問題。
<strong>1.4.1 理解ActionResult</strong>
Action Result把“指明意圖”和“執(zhí)行意圖”分離了。不是直接使用Response對象,而是返回一個派生于ActionResult類的對象,但這是間接發(fā)生的,不直接生成響應(yīng)。
public ActionResult ProduceOutput()
{
// return new RedirectResult("/Basic/Index");
return Redirect("/Basic/Index");
}
MVC框架包含了很多內(nèi)建的ActionResult類型:


<strong>1.4.2 通過渲染View返回HTML</strong>
public ActionResult Index()
{
return View("Homepage");// Homepage作為View的名稱傳遞
}
<strong>1.4.3 將數(shù)據(jù)從Action傳遞給視圖</strong>
1.可以把一個對象作為View方法的參數(shù)發(fā)送給View:
public ActionResult Index()
{
DateTime date = DateTime.Now;
return View(date);
}
然后用Razor的Model關(guān)鍵字訪問這個對象。
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is :@((DateTime)Model).DayOfWeek
最好采用下面的強類型視圖:
@model DateTime
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
the day is :@Model.DayOfWeek
2.使用ViewBag傳遞數(shù)據(jù)
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
the day is : @ViewBag.Date.DayOfWeek
<p />
The message is : @ViewBag.Message
<strong>1.4.4 執(zhí)行重定向</strong>
1.重定向到字面URL(臨時重定向)
public RedirectResult Redirect()
{
return Redirect("/Example/Index");
}
2.重定向到路由系統(tǒng)的URL
public RedirectToRouteResult Redirect()
{
return RedirectToRoute(new { controller = "Example", action = "Index", ID = "MyID" });
}
3.重定向到一個Action方法
這只是對于RedirectToRoute方法的封裝。
public RedirectToRouteResult Redirect()
{
return RedirectToAction("Index");
}
如果只有一個參數(shù),它假設(shè)你指向的時當前Controller的Action方法,若要重定向到另一個Controller:
return RedirectToAction("Index", "Basic");
<strong>1.4.5 返回錯誤及HTTP代碼</strong>
public HttpStatusCodeResult StatusCode()
{
return new HttpStatusCodeResult(404, "URL cannot be serviced");
return HttpNotFound();
}