Consume Web API in same app from MVC Controller



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I'm writing an application that serves both Web API and a MVC content in .NET Core. From the MVC Controller I want to call the API's functions and receive the data that comes back. Is there a better way than using HttpClient or similar things? Like instantiating the API's controller class in the MVC Controller? I can't just go new ApiController(); since it's depending on dependency injection, can I?










share|improve this question
























  • Duplicate: stackoverflow.com/questions/53322147/…

    – Adrian K
    Nov 15 '18 at 21:21

















3















I'm writing an application that serves both Web API and a MVC content in .NET Core. From the MVC Controller I want to call the API's functions and receive the data that comes back. Is there a better way than using HttpClient or similar things? Like instantiating the API's controller class in the MVC Controller? I can't just go new ApiController(); since it's depending on dependency injection, can I?










share|improve this question
























  • Duplicate: stackoverflow.com/questions/53322147/…

    – Adrian K
    Nov 15 '18 at 21:21













3












3








3


2






I'm writing an application that serves both Web API and a MVC content in .NET Core. From the MVC Controller I want to call the API's functions and receive the data that comes back. Is there a better way than using HttpClient or similar things? Like instantiating the API's controller class in the MVC Controller? I can't just go new ApiController(); since it's depending on dependency injection, can I?










share|improve this question
















I'm writing an application that serves both Web API and a MVC content in .NET Core. From the MVC Controller I want to call the API's functions and receive the data that comes back. Is there a better way than using HttpClient or similar things? Like instantiating the API's controller class in the MVC Controller? I can't just go new ApiController(); since it's depending on dependency injection, can I?







asp.net-core-mvc asp.net-core-webapi






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 16:15









phuzi

4,80012036




4,80012036










asked Nov 15 '18 at 14:54









lennyylennyy

154119




154119












  • Duplicate: stackoverflow.com/questions/53322147/…

    – Adrian K
    Nov 15 '18 at 21:21

















  • Duplicate: stackoverflow.com/questions/53322147/…

    – Adrian K
    Nov 15 '18 at 21:21
















Duplicate: stackoverflow.com/questions/53322147/…

– Adrian K
Nov 15 '18 at 21:21





Duplicate: stackoverflow.com/questions/53322147/…

– Adrian K
Nov 15 '18 at 21:21












2 Answers
2






active

oldest

votes


















4















"Like instantiating the API's controller class in the MVC Controller?"




No, that's not a good idea, and wouldn't really work anyway. You could make a HTTP request as you mentioned, but it's not that efficient if everything is already part of the application.



But if the functionality which actually gets the data is in a separate class, you could call the relevant method of that class and get the data directly - probably better to get it as a variable than as JSON anyway.



This all about your application design - the process of retrieving the data (e.g. from a database) should be functionally separate from the process of providing it to the user (e.g. as JSON via an API controller). So any code should be able to call the data retrieval functionality, not just the API.



Conceptually you might like to think of these as different layers of functionality. It's a common architectural model in software - a presentation layer, (optionally) a logic layer and a data layer.






share|improve this answer

























  • how would I go about connecting to the database in that seperate class? Can I use DI there, too?

    – lennyy
    Nov 21 '18 at 14:24











  • I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

    – ADyson
    Nov 21 '18 at 15:30


















3














If your application has both ASP.Net MVC and Web API parts then best solution would be to put any functionality/business logic required to get any data in a service layer. Then both MVC and Web API can consume these directly rather than having to fire up a HttpClient (or similar).



To be honest, this is a good practice anyway whether you have both MVC and Web API, or just one. This would allow different API/MVC controllers to access the same data in the application without having to duplicate code.



Take a look at Onion Architecture which typically has 4 layers.



  1. UI (MVC/Web API),

  2. Service layer (for all business logic),

  3. Repository layer (for persistence of data) and,

  4. A common base layer for all your domain entities (POCOs that are common across all layers).

There's a good article over on C# Corner that explains Onion Architecture.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322147%2fconsume-web-api-in-same-app-from-mvc-controller%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4















    "Like instantiating the API's controller class in the MVC Controller?"




    No, that's not a good idea, and wouldn't really work anyway. You could make a HTTP request as you mentioned, but it's not that efficient if everything is already part of the application.



    But if the functionality which actually gets the data is in a separate class, you could call the relevant method of that class and get the data directly - probably better to get it as a variable than as JSON anyway.



    This all about your application design - the process of retrieving the data (e.g. from a database) should be functionally separate from the process of providing it to the user (e.g. as JSON via an API controller). So any code should be able to call the data retrieval functionality, not just the API.



    Conceptually you might like to think of these as different layers of functionality. It's a common architectural model in software - a presentation layer, (optionally) a logic layer and a data layer.






    share|improve this answer

























    • how would I go about connecting to the database in that seperate class? Can I use DI there, too?

      – lennyy
      Nov 21 '18 at 14:24











    • I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

      – ADyson
      Nov 21 '18 at 15:30















    4















    "Like instantiating the API's controller class in the MVC Controller?"




    No, that's not a good idea, and wouldn't really work anyway. You could make a HTTP request as you mentioned, but it's not that efficient if everything is already part of the application.



    But if the functionality which actually gets the data is in a separate class, you could call the relevant method of that class and get the data directly - probably better to get it as a variable than as JSON anyway.



    This all about your application design - the process of retrieving the data (e.g. from a database) should be functionally separate from the process of providing it to the user (e.g. as JSON via an API controller). So any code should be able to call the data retrieval functionality, not just the API.



    Conceptually you might like to think of these as different layers of functionality. It's a common architectural model in software - a presentation layer, (optionally) a logic layer and a data layer.






    share|improve this answer

























    • how would I go about connecting to the database in that seperate class? Can I use DI there, too?

      – lennyy
      Nov 21 '18 at 14:24











    • I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

      – ADyson
      Nov 21 '18 at 15:30













    4












    4








    4








    "Like instantiating the API's controller class in the MVC Controller?"




    No, that's not a good idea, and wouldn't really work anyway. You could make a HTTP request as you mentioned, but it's not that efficient if everything is already part of the application.



    But if the functionality which actually gets the data is in a separate class, you could call the relevant method of that class and get the data directly - probably better to get it as a variable than as JSON anyway.



    This all about your application design - the process of retrieving the data (e.g. from a database) should be functionally separate from the process of providing it to the user (e.g. as JSON via an API controller). So any code should be able to call the data retrieval functionality, not just the API.



    Conceptually you might like to think of these as different layers of functionality. It's a common architectural model in software - a presentation layer, (optionally) a logic layer and a data layer.






    share|improve this answer
















    "Like instantiating the API's controller class in the MVC Controller?"




    No, that's not a good idea, and wouldn't really work anyway. You could make a HTTP request as you mentioned, but it's not that efficient if everything is already part of the application.



    But if the functionality which actually gets the data is in a separate class, you could call the relevant method of that class and get the data directly - probably better to get it as a variable than as JSON anyway.



    This all about your application design - the process of retrieving the data (e.g. from a database) should be functionally separate from the process of providing it to the user (e.g. as JSON via an API controller). So any code should be able to call the data retrieval functionality, not just the API.



    Conceptually you might like to think of these as different layers of functionality. It's a common architectural model in software - a presentation layer, (optionally) a logic layer and a data layer.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 15 '18 at 16:16









    phuzi

    4,80012036




    4,80012036










    answered Nov 15 '18 at 15:04









    ADysonADyson

    25.7k112746




    25.7k112746












    • how would I go about connecting to the database in that seperate class? Can I use DI there, too?

      – lennyy
      Nov 21 '18 at 14:24











    • I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

      – ADyson
      Nov 21 '18 at 15:30

















    • how would I go about connecting to the database in that seperate class? Can I use DI there, too?

      – lennyy
      Nov 21 '18 at 14:24











    • I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

      – ADyson
      Nov 21 '18 at 15:30
















    how would I go about connecting to the database in that seperate class? Can I use DI there, too?

    – lennyy
    Nov 21 '18 at 14:24





    how would I go about connecting to the database in that seperate class? Can I use DI there, too?

    – lennyy
    Nov 21 '18 at 14:24













    I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

    – ADyson
    Nov 21 '18 at 15:30





    I don't see any reason why not. Dependency injection is just a design pattern, it's not tied to any specific technology (such as web API)

    – ADyson
    Nov 21 '18 at 15:30













    3














    If your application has both ASP.Net MVC and Web API parts then best solution would be to put any functionality/business logic required to get any data in a service layer. Then both MVC and Web API can consume these directly rather than having to fire up a HttpClient (or similar).



    To be honest, this is a good practice anyway whether you have both MVC and Web API, or just one. This would allow different API/MVC controllers to access the same data in the application without having to duplicate code.



    Take a look at Onion Architecture which typically has 4 layers.



    1. UI (MVC/Web API),

    2. Service layer (for all business logic),

    3. Repository layer (for persistence of data) and,

    4. A common base layer for all your domain entities (POCOs that are common across all layers).

    There's a good article over on C# Corner that explains Onion Architecture.






    share|improve this answer





























      3














      If your application has both ASP.Net MVC and Web API parts then best solution would be to put any functionality/business logic required to get any data in a service layer. Then both MVC and Web API can consume these directly rather than having to fire up a HttpClient (or similar).



      To be honest, this is a good practice anyway whether you have both MVC and Web API, or just one. This would allow different API/MVC controllers to access the same data in the application without having to duplicate code.



      Take a look at Onion Architecture which typically has 4 layers.



      1. UI (MVC/Web API),

      2. Service layer (for all business logic),

      3. Repository layer (for persistence of data) and,

      4. A common base layer for all your domain entities (POCOs that are common across all layers).

      There's a good article over on C# Corner that explains Onion Architecture.






      share|improve this answer



























        3












        3








        3







        If your application has both ASP.Net MVC and Web API parts then best solution would be to put any functionality/business logic required to get any data in a service layer. Then both MVC and Web API can consume these directly rather than having to fire up a HttpClient (or similar).



        To be honest, this is a good practice anyway whether you have both MVC and Web API, or just one. This would allow different API/MVC controllers to access the same data in the application without having to duplicate code.



        Take a look at Onion Architecture which typically has 4 layers.



        1. UI (MVC/Web API),

        2. Service layer (for all business logic),

        3. Repository layer (for persistence of data) and,

        4. A common base layer for all your domain entities (POCOs that are common across all layers).

        There's a good article over on C# Corner that explains Onion Architecture.






        share|improve this answer















        If your application has both ASP.Net MVC and Web API parts then best solution would be to put any functionality/business logic required to get any data in a service layer. Then both MVC and Web API can consume these directly rather than having to fire up a HttpClient (or similar).



        To be honest, this is a good practice anyway whether you have both MVC and Web API, or just one. This would allow different API/MVC controllers to access the same data in the application without having to duplicate code.



        Take a look at Onion Architecture which typically has 4 layers.



        1. UI (MVC/Web API),

        2. Service layer (for all business logic),

        3. Repository layer (for persistence of data) and,

        4. A common base layer for all your domain entities (POCOs that are common across all layers).

        There's a good article over on C# Corner that explains Onion Architecture.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 15:10

























        answered Nov 15 '18 at 15:03









        phuziphuzi

        4,80012036




        4,80012036



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53322147%2fconsume-web-api-in-same-app-from-mvc-controller%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20