ASP.NET 4 Developer preview中的异步操纵器
在放弃了对.NET 3的支持之后, ASP.NET MVC 4 彻底拥抱了Task类库, 你不需求再蛋疼的给每个Action写两个方法, 也无需傻傻的手动对异步Action计数器增减了(AsyncManager.OutstandingOperations.Increment()), 现在的你只需拿起手指, 轻轻敲几下, 其他的事情都由系统帮你完成.
- public class PortalController : AsyncController {
- public Task News( stringcity) {
- returnTask.Factory.StartNew(() => RunThread(city)).ContinueWith(t => {
- returnView(newViewStringModel(){Text = t.Result});
- });}
- privatestringRunThread(stringinput){
- Thread.Sleep(5000);
- returninput;}
- }
是否好多了?Lamda可以让一切更爽:
- public Task News(stringcity) {
- returnTask.Factory.StartNew(() => RunThread(city)).ContinueWith(t => View(newViewStringModel{
- Text = t.Result }));
- }
asp.net mvc 4.0 更简洁的写法 是否好多了?Lamda可以让一切更爽:
public class Default1Controller : AsyncController
{
TRSLMS.Data.Models.DataDbContext db = new Data.Models.DataDbContext();
//
// GET: /HomePage/Default1/
public Task<ViewResult> FooBar()
{
// StartNew 开始执行 返回action 或则直接返回数据 ContinueWith 结束后返回值
return Task.Factory.StartNew(() => { return db.USER.ToList(); }).ContinueWith(t =>{return View(t.Result);});
}
}
时间: 2024-11-07 01:26:21