0

我有 2 个控制器CustomerControllerPrivatemessageController

客户有一个不作为的方法

private readonly ICustomerService _customerService; 
public Customer(....)   << autofac Ioc 
{
 }

    [NonAction]
    protected CustomerNavigationModel GetCustomerNavigationModel(Customer customer)
    {
        var model = new CustomerNavigationModel();

        .... _customerSerice...
        return model;
    }

我想从中获得GetCustomerNavigationModel价值,CustomerController因为我不想为 重新创建相同的方法PrivateMessageController,这在 ASP.NET MVC 3.0 中是否可行?

4

2 回答 2

1

这里有两个选择。

  1. 将方法设为公共静态:

        [NonAction]
        public static CustomerNavigationModel GetCustomerNavigationModel(Customer customer)
        {
            var model = new CustomerNavigationModel();
    
            .... _customerSerice...
            return model;
        }
    
  2. 创建一个基本控制器并在那里实现该方法。让您的两个控制器都从您的基本控制器派生:

    public abstract class MyBaseController : Controller
    {
        [NonAction]
        protected CustomerNavigationModel GetCustomerNavigationModel(Customer customer)
        {
            var model = new CustomerNavigationModel();
    
            .... _customerSerice...
            return model;
        }
    }
    
    
    public class CustomerController : MyBaseController
    {
        ....
    }
    
    public class PrivatemessageController : MyBaseController
    {
        ....
    }
    
于 2012-08-05T09:20:29.383 回答
0

将该方法重构为一个单独的类,并从您的控制器中使用该类。您也可以将方法设为静态,然后使用以下方法从PrivatemessageController调用它:

CustomerController.GetCustomerNavigationModel(customer);
于 2012-08-05T09:20:19.240 回答