控制器主要负责响应用户的输入,并且在响应时修改模型(Model)。
控制器操作的工作是响应URL请求,执行正确的操作,并向浏览器或是单击这个URL的用户用户做出响应。
例子
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public string Browse(string genre) {
string message = HttpUtility.HtmlEncode("Store.Browse,Gentre="+genre);
return message;
}
利用HttpUtility.HtmlEncode来预处理用户输入,这样能阻止用户向链接视图只不过中注入javascript代码或HTML标记。
例如:/Home/Browse?Genre=<script>window.location=%27http://www.baidu.com%27</script>