代码改变世界

ASP.NETMVC3RC的一些新特性 (2010.11.9发布版)

2010-11-27 21:02  撞破南墙  阅读(2617)  评论(9编辑  收藏  举报

1控制Controller的SESSION

 [ControllerSessionState(SessionStateBehavior.Disabled)]

//禁用SESSION

public class CoolController : Controller {

  public ActionResult Index() {

object o = Session["Key"]; // 触发异常

  }

}

 [ControllerSessionState(SessionStateBehavior.ReadOnly)]

public class CoolController : Controller {

  public ActionResult Index() {

Session["Key"] = "value"; // SESSION属于只读

  }

}

2新的验证属性

2.1比较Compare

public class User {

    [Required]

    public string Password { get; set; }

    [Required, Compare("Password")]

    public string ComparePassword { get; set; }

}

2.2 控制客户端属性

UserName 属性被赋予UserNameAvailable,当username属于被编辑页面的时候,客户端验证将调用UserNameAvailable 方法

public class User {

    [Remote("UserNameAvailable", "Users")]

    public string UserName { get; set; }

}

The following example shows the corresponding controller.

public class UsersController {

    public bool UserNameAvailable(string username) {

        return !MyRepository.UserNameExists(username);

    }

}

2.3LabelFor和LabelForModel的新的重载函数

 @Html.LabelFor(m => m.PropertyName, "Label Text");

@Html.LabelForModel("Label Text");

3action可以使用缓存

当前时间: @DateTime.Now

被缓存时候的事件: @Html.Action("GetDate")

The GetDate action is annotated with the OutputCacheAttribute:

[OutputCache(Duration = 100, VaryByParam = "none")]

public string GetDate() {

    return DateTime.Now.ToString();

}

4"Add View" 的对话框变干净了。

不会跟以前一样把整个.NET FRAMEWORK 的类 都包含进去。

5 更小粒度的验证Granular Request Validation

跳过验证。

这次是粒度到了某个属性

public class BlogPostViewModel {  

    [SkipRequestValidation]

    public string Description {get; set;}

}

之前的版本的粒度是整个函数

[HttpPost]

[ValidateInput(false)]

public ActionResult Edit(BlogPostViewModel post) {

    // Save the post in the database