大家好,我是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宣布了两次
答案 0 :(得分:2)
如果在多个控制器上使用相同的模式,您可能需要考虑创建一个处理此功能的控制器基类。
答案 1 :(得分:0)
我假设两个服务类都是从相同的接口派生的,例如。 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方法。