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

相关文章:

  • 2022-02-25
  • 2022-12-23
  • 2021-07-27
  • 2022-02-16
  • 2021-08-23
  • 2021-12-25
  • 2021-11-20
猜你喜欢
  • 2021-09-12
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2021-08-17
  • 2021-07-27
  • 2021-06-30
相关资源
相似解决方案