将
视图返回到控制器操作的最佳方法是什么? 在性能、易于测试、优化等方面。
return View();
或
return View("[ActionName]");
然后
return View([model]);
与
return View("[ActionName]",[model]);
所以我运行了一个小基准测试,这就是我得到的:
readonly Stopwatch sw = new Stopwatch();
public void Benchmark()
{
var t1 = Test(View);
var t2 = Test(() => View("[ActionName]"));
var t4 = Test(() => View(new Model()));
var t3 = Test(() => View("[ActionName]", new Model()));
string result = $"{t1} - {t2} - {t3} - {t4}";
//Results:
//4466 - 4856 - 6969 - 6977
//4551 - 4986 - 7070 - 7056
//5181 - 5263 - 7142 - 7864
}
public long Test(Func<ViewResult> f)
{
sw.Start();
for (int i = 0; i < 100000000; i++)
{ var x = f(); }
sw.Stop();
long t = sw.ElapsedMilliseconds;
sw.Reset();
return t;
}
所以前两个似乎快了一点...但是,差异是微不足道的。使用最适合您的方法。