【发布时间】:2013-11-24 17:03:20
【问题描述】:
我尝试改进以下代码。问题是Handle方法变得繁琐。我正在寻找一种从主要方法中排除添加和处理命令的方法。我想ActionResult HandleCommand 方法通过添加新命令变得对更改关闭。所以,我对大开关块并不感到兴奋。我很乐意收到任何建议。
[HttpPost]
public ActionResult HandleCommand(string command)
{
switch (command)
{
case "foo":
DoSomthing();
return View("someView1");
case "bar":
DoSomthingElse();
return RedirectToAction("someAction");
case "fooBar":
return File("file.txt", "application");
//...
default:
//...
return new HttpStatusCodeResult(404);
}
}
【问题讨论】:
标签: c# asp.net-mvc refactoring switch-statement