简体   繁体   中英

Why I can't use the BaseController Initialize() method in this controller class?

I am pretty new in .NET (I came from Java EE ecosystem) and I have the following doubt. Into the project on which I am working on I have a controller class extending the ApiController abstract class:

public class MailProtocolloController : ApiController
{
    private String urlBaseProtocolloApi = "http://XXX.westeurope.cloudapp.azure.com:8080";

    private NetworkCredential myCreds;
    private CredentialCache credCache;

    public MailProtocolloController()
    {
        var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext.Current);
        using (var contextCurrentUser = spContext.CreateUserClientContextForSPHost())
        {
            PecMailAdminContext dbContext = new PecMailAdminContext();
            Casella casella = Casella.GetCasellaFromSito(dbContext, spContext.SPHostUrl.ToString());
            string userServizio = casella.Username;
            string passwordUserServizio = MailManagerUtilities.Utils.Decrypt(casella.Password);
            credCache = new CredentialCache();
        }
    }

    ..........................................................................................................
    ..........................................................................................................
    ..........................................................................................................
}

As you can see in the previous code I am retrieving the information about a specific user directly into the constructor (it have to be the first thing done because then this users will be use in all the controller method handling request to perform external HTTP request to external WS.

I think that do it into the constructor is pretty awful because the constructor have only to set variable values from paramether and have not to implement real logic.

So I was thinking to move this logico into an initialization method. I know that in theory my controller class should implement the BaseController inteface and then implement this initialization method that will contain this logic:

protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
    // RETRIEVE THE SERVICE USER HERE
}

The problem is that I was trying to do something like this:

public class MailProtocolloController : ApiController, BaseController
{
    ......................................................
    ......................................................
    ......................................................
}

but Visual Studio give me an error on the declaration of this BaseController in the method declaration, the error is:

The type or namespace name 'BaseController' could not be found (are you missing a using directive or assembly reference?).

I can't import this interface definition.

What is the problem? What am I missing? How can I try to fix it or use an initialization method in some other way into my controller?

This happens when your MailProtocolloController is in a different namespace to BaseController . The solution, either add a using BaseControllerNamespace; directive to the top of your .cs file that contains MailProtocolloController (similar to import in Java) or provide a fully qualified type name for BaseController , for example:

public class MailProtocolloController : ApiController, BaseControllerNamespace.BaseController

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM