Windows Azure Toolkit for Social Games(三)

来源:互联网 发布:日本对女生的审美知乎 编辑:程序博客网 时间:2024/06/06 01:33

了解过mvc大体过程后,拿到一个画面来分析下。

默认的处理是交由Home Controller的Index方法。

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

View的默认路径是View/Home/Index.cshtml

打开一看,代码很简单。主要是根据Request.IsAuthenticated,判断显示内容还是显示认证Link。

@{
    ViewBag.Title = "Index";
}
<h2>Home</h2>
<p>
This site showcases two sample games that consumes the operations exposed by a generic game play service API. The game play service is responsible for handling the generic game operations like user authentication, joining multiple players to a game, and persisting the game live state. 
</p>
@if (Request.IsAuthenticated)
{
<p>
    Select an option from the menu to start playing with the samples, see your profile and friends, and check your score on the leaderboard!
</p>
}
else
{
<p>
    Click @Html.ActionLink("Log On", "LogOn", "Account") to authenticate.
</p>
}

感觉代码比实际看到的内容要少。那是因为有一个~/Views/Shared/_Layout.cshtml套在外面。等同于MastePage的作用

从下图的说明可以看出只要在_ViewStart.cshtml设定一次,就不需要在每一个画面上指定_Layout.cshtml的路径了。


总体来说,这个Index画面还是很简单的,要注意的有两点:

1,ViewBag.Title = "Index";

ViewBag是View层里一个很好用的容器,你可以随时添加内容。而不需要指定类型。

2,Request.IsAuthenticated

Azure里没法用Sesion(因为每次处理的WebRoll是不同的),用户认证与否就靠Request.IsAuthenticated

Request是一个HttpRequestBase对象。

以上两点的生命周期不明,按理说Request.IsAuthenticated只能是一次Request,但它肩负着Session的作用。