Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 3 在MVC3中使用类_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 3 在MVC3中使用类

Asp.net mvc 3 在MVC3中使用类,asp.net-mvc-3,Asp.net Mvc 3,大家好,我是MVC3的初学者。我的问题是关于如何使用课堂。我正在使用web服务获取参数,在这一点上,我没有问题,但我的问题是,我正在一次又一次地定义每个控制器方法中的每个连接,所以是否存在任何shorcut 以下是一个例子: public ActionResult BeyanListe() { Services_building client = new Services_built(); Services client_service = new Se

大家好,我是MVC3的初学者。我的问题是关于如何使用课堂。我正在使用web服务获取参数,在这一点上,我没有问题,但我的问题是,我正在一次又一次地定义每个控制器方法中的每个连接,所以是否存在任何shorcut

以下是一个例子:

public ActionResult BeyanListe()
        {

Services_building client = new Services_built();
            Services client_service = new Services();
            client_service.ClientCredentials.UserName.UserName = "service_test";
            client_service.ClientCredentials.UserName.Password = "..";
            client_service.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
            client.ClientCredentials.UserName.UserName = "service_test";
            client.ClientCredentials.UserName.Password = "..";
            client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;

return View()
}
正如您所看到的,我已经为clientcredentials声明了两次

  • 使客户端服务和客户端成为控制器的受保护或私有成员
  • 将初始化代码移到控制器的构造函数中
  • 根据需要在您的操作中使用客户机服务和客户机

  • 如果您在多个控制器上使用相同的模式,您可能需要考虑创建一个控制器基类来处理这个函数。

    < P>我假定这两个服务类都是从同一个接口派生的,eg. IServices,然后:

    private void AssignCredentials(IServices services)
    {
       services.ClientCredentials.UserName.UserName = "service_test";
       services.ClientCredentials.UserName.Password = "..";
       services.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None;
    }
    
    在基本方法中调用AssignCredentials方法