C# 在MVC控制器中从多个操作运行单个方法

C# 在MVC控制器中从多个操作运行单个方法,c#,model-view-controller,controller,C#,Model View Controller,Controller,我有以下代码,这些代码在控制器中的多个操作方法中是相同的。是否有可能将其简化为一个方法,并将多个操作传递给它 [HttpGet] public ActionResult Tool2(Guid? id) { var model = _viewModelFactory.CreateViewModel<Guid?, ToolsViewModel>(id); return model.ReferenceFound ? View(model) : View("~/Views/

我有以下代码,这些代码在控制器中的多个操作方法中是相同的。是否有可能将其简化为一个方法,并将多个操作传递给它

[HttpGet]
public ActionResult Tool2(Guid? id)
{
    var model = _viewModelFactory.CreateViewModel<Guid?, ToolsViewModel>(id);

    return model.ReferenceFound ? View(model) : View("~/Views/Tools/InvalidReference.cshtml", model);
}

[HttpGet]
public ActionResult Tool1(Guid? id)
{
    var model = _viewModelFactory.CreateViewModel<Guid?, ToolsViewModel>(id);

    return model.ReferenceFound ? View(model) : View("~/Views/Tools/InvalidReference.cshtml", model);
}
[HttpGet]
公共操作结果工具2(Guid?id)
{
var model=_viewModelFactory.CreateViewModel(id);
返回model.referencefund?视图(model):视图(“~/Views/Tools/InvalidReference.cshtml”,model);
}
[HttpGet]
公共操作结果工具1(Guid?id)
{
var model=_viewModelFactory.CreateViewModel(id);
返回model.referencefund?视图(model):视图(“~/Views/Tools/InvalidReference.cshtml”,model);
}

每个操作都有一个唯一的视图,需要保留该视图。

创建一个通用方法,两个操作都将调用该方法。将这两个操作分开,因为理解自定义路由比编写(和读取!)自定义路由更清晰

public ActionResult Tool1(Guid? guid)
{
    return CommonAction(guid, "Tool1");
}

public ActionResult Tool2(Guid? guid)
{
    return CommonAction(guid, "Tool2");
}

private ActionResult CommonAction(Guid? guid, string viewName)
{
    var model = _viewModelFactory.CreateViewModel<Guid?, ToolsViewModel>(id);
    return model.ReferenceFound ?
        View(model) : View("~/Views/Tools/InvalidReference.cshtml", model);
}
公共操作结果工具1(Guid?Guid) { 返回CommonAction(guid,“Tool1”); } 公共操作结果工具2(Guid?Guid) { 返回CommonAction(guid,“Tool2”); } 私有ActionResult CommonAction(Guid?Guid,字符串viewName) { var model=_viewModelFactory.CreateViewModel(id); 返回model.referenceFund? 视图(模型):视图(“~/Views/Tools/InvalidReference.cshtml”,模型); }
创建两个操作都将调用的通用方法。将这两个操作分开,因为理解自定义路由比编写(和读取!)自定义路由更清晰

public ActionResult Tool1(Guid? guid)
{
    return CommonAction(guid, "Tool1");
}

public ActionResult Tool2(Guid? guid)
{
    return CommonAction(guid, "Tool2");
}

private ActionResult CommonAction(Guid? guid, string viewName)
{
    var model = _viewModelFactory.CreateViewModel<Guid?, ToolsViewModel>(id);
    return model.ReferenceFound ?
        View(model) : View("~/Views/Tools/InvalidReference.cshtml", model);
}
公共操作结果工具1(Guid?Guid) { 返回CommonAction(guid,“Tool1”); } 公共操作结果工具2(Guid?Guid) { 返回CommonAction(guid,“Tool2”); } 私有ActionResult CommonAction(Guid?Guid,字符串viewName) { var model=_viewModelFactory.CreateViewModel(id); 返回model.referenceFund? 视图(模型):视图(“~/Views/Tools/InvalidReference.cshtml”,模型); }
我已经这样做了,但是我真的只想要一个方法,而不是2调用另一个。我已经这样做了,但是我真的只想要一个方法,而不是2调用另一个。