我想完全了解我如何简化如下:将参数传递给Action?
public ActionResult Create(string ds) {
InitializeServices(ds, "0000");
vm.Account = new Account {
PartitionKey = "0000",
RowKey = "0000",
Created = DateTime.Now,
CreatedBy = User.Identity.Name
};
}
catch (ServiceException ex) {
ModelState.Merge(ex.Errors);
}
catch (Exception e) {
Trace.Write(e);
ModelState.AddModelError("", "Database access error: " + e.Message);
}
return View("CreateEdit", vm);
}
我有几个伟大的答案和以下建议:
private void HandleException(Action action) {
try {
action();
}
catch (ServiceException ex) {
ModelState.Merge(ex.Errors);
}
catch (Exception e)
{
Trace.Write(e);
ModelState.AddModelError("", "Database access error: " + e.Message);
}
}
RunAndHandleExceptions(new Action(() =>
{
//Do some computing }
));
这看起来像一个真正伟大的解决方案,但我仍然不明白如何将我的 参数传递到动作中。我需要做的是在下面的经过:
string ds
System.Web.Mvc.ModelState ModelState (passed as a reference)
请注意,捕获每个异常是一种糟糕的编程习惯。有人调用堆栈可能已经能够处理该异常。 – 2011-12-19 16:29:39