locked
MVC and Web Api infrastructure RRS feed

  • Question

  • User52625461 posted

    Hello All,

    I want to use MVC and Web API together and want the infrastructure as follows :

    These all projects should be independent

    1. MVC project

    ---- Here only views should be there. A view/response should be returned from Web API project

    2. Web API Project

    ---- Here only controllers and models should be present. 

    3, DAL Project

    ---- Database essentials

    So, how can make this infrastructure ? Can I call the controller residing in Web API project from MVC project ?

    Thanks !

    Friday, September 26, 2014 2:41 AM

All replies

  • User541108374 posted

    Hi,

    Here only views should be there.

    Perhaps it's better to replace them with simple html pages as it feels you're trying to a SPA.

    Can I call the controller residing in Web API project from MVC project

    Yes you can call your endpoints. Concerning deployment: wouldn't it be better if you make up one project with both MVC and your Web API endpoints in it?

    Grz, Kris.

    Friday, September 26, 2014 3:42 AM
  • User52625461 posted

    rohitpundlik

    Can I call the controller residing in Web API project from MVC project

    Yes you can call your endpoints. Concerning deployment: wouldn't it be better if you make up one project with both MVC and your Web API endpoints in it?

    Grz, Kris.

    Yes, was thinking on the same line.; thanks for the good suggestion.  Can you please give me some example/s which will explain this implementation ?

    Thanks !

    Friday, September 26, 2014 4:16 AM
  • User541108374 posted

    Hi,

     Can you please give me some example/s which will explain this implementation ?

    What kind of examples do you need/want?

    When you create a new MVC project you can also select the checkbox to add Web API to the project:

    Grz, Kris.

    Friday, September 26, 2014 8:48 AM
  • User52625461 posted

    XIII

    Hi,

    rohitpundlik

     Can you please give me some example/s which will explain this implementation ?

    What kind of examples do you need/want?

    Grz, Kris.

    Means any Sample code OR link which will show  how to call web API from MVC view ? How can I club together MVC+web api + scaffolding ?

    Monday, September 29, 2014 12:29 AM
  • User541108374 posted

    Hi,

    Means any Sample code OR link which will show  how to call web API from MVC view ? How can I club together MVC+web api + scaffolding ?

    For all Web API tutorials go to http://www.asp.net/web-api.

    How you could call it from a controller: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client.

    How to call it from the client (rendered MVC View or html): http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-1.

    Grz, Kris.

    Monday, September 29, 2014 3:16 AM
  • User52625461 posted

    Hi,

    rohitpundlik

    Means any Sample code OR link which will show  how to call web API from MVC view ? How can I club together MVC+web api + scaffolding ?

    For all Web API tutorials go to http://www.asp.net/web-api.

    How you could call it from a controller: http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client.

    How to call it from the client (rendered MVC View or html): http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-1.

    Grz, Kris.

    Hey thanks Kris,

    I have seen those links.. Good one...

    Just one more question Can I mix MVC+web api+ scaffolding ?

    In this case there will be 2 controllers(one from api and second is from scaffolding).. So how can i handle this scenario ?

    Monday, September 29, 2014 3:23 AM
  • User541108374 posted

    Hi,

    how can i handle this scenario ?

    Create them separately by using scaffolding. You could use the same datacontext if needed.

    When needed you can create your own scaffolder: http://blogs.msdn.com/b/webdev/archive/2014/04/03/creating-a-custom-scaffolder-for-visual-studio.aspx.

    Grz, Kris.

    Monday, September 29, 2014 3:30 AM
  • User52625461 posted

    Ok got it 

    Hi,

    rohitpundlik

    how can i handle this scenario ?

    Create them separately by using scaffolding. You could use the same datacontext if needed.

    When needed you can create your own scaffolder: http://blogs.msdn.com/b/webdev/archive/2014/04/03/creating-a-custom-scaffolder-for-visual-studio.aspx.

    Grz, Kris.

    Ok got it Kris..

    After scaffolding I got below 2 controllers with methods

    MVC Controller..

     public async Task<ActionResult> Create([Bind(Include="Id,Name,Age")] Person person) {....}
    public async Task<ActionResult> Edit(int? id){.....}
    public async Task<ActionResult> Delete(int? id){....}
    

    and webapi controller....

    public async Task<IHttpActionResult> GetPerson(int id){....}
    public async Task<IHttpActionResult> PutPerson(int id, Person person){....}
    public async Task<IHttpActionResult> DeletePerson(int id){....}

    Both these controllers are generated by scaffolding...  they have some common method and Unique methods also...

    then whats the use by keeping the methods in separate controller instead of putting them in web api controller ?

    Monday, September 29, 2014 4:40 AM
  • User541108374 posted

    Hi,

    whats the use by keeping the methods in separate controller instead of putting them in web api controller ?

    Depends on what you want to do with it. Likely you have an MVC controller that spits out views to the browser and you use your Web API controller with the same names to interact via ajax/json to the backed from the rendered views (from MVC).

    Grz, Kris.

    Monday, September 29, 2014 5:06 AM
  • User52625461 posted

    Hi,

    rohitpundlik

    whats the use by keeping the methods in separate controller instead of putting them in web api controller ?

    Depends on what you want to do with it. Likely you have an MVC controller that spits out views to the browser and you use your Web API controller with the same names to interact via ajax/json to the backed from the rendered views (from MVC).

    Grz, Kris.

    Ok, to keep it simple... I am trying to keep the architecture as follows

    MVC Controller

     public ActionResult Delete(int? id)
    {
    //Here I will call the method from web api controller
    }

    WebApi Controller

     public IHttpActionResult DeletePerson(int id)
    {
    // Actual logic
    }

    and will return data/response to MVC controller and then View will be rendered.

    Is that fine ? Am i going in a correct way ?

    Monday, September 29, 2014 5:24 AM
  • User541108374 posted

    Hi,

    and will return data/response to MVC controller and then View will be rendered.

    That seems weird. If you want to call your Web API from your controller you can also directly call the backend from your MVC controller. Web API is usually used to be called from a client otherwise it'll only generate overhead.

    If you need/want to expose common data as well on your MVC as Web API part then put that in a common project which you reference from both but not with the extra step of calling a Web API from your controller.

    Scenario I would suggest:

    • MVC controller gets essential data to make up your view
    • view gets rendered in the browser
    • if you have data exchange, no html, to update something on the client (browser in your case) you do that via ajax to a Web API endpoint

    Grz, Kris.

    Monday, September 29, 2014 5:32 AM
  • User52625461 posted

    As we want to use the same infrastructure in mobile applications that why we made a thought of it..

    Also, I found kind of same solution on one of the link

    http://forums.asp.net/t/2010883.aspx?Call+Web+API+in+MVC

    Thats why we are curios about this approach !!

    Monday, September 29, 2014 5:54 AM
  • User541108374 posted

    Hi,

    basically what most of them are saying is what I'm saying: don't call it from your MVC controller as it'll create a slowdown/bottleneck. I can understand you interpreted by Fuxiang as such but he's simply replying to how you can do what you ask. What lacks is good practice and well thought ways. In the end you want to eliminate all slow parts of your application or don't even create any in the first place.

    On that part of using the same infrastructure: do you want to create a responsive site to show in a mobile browser or create dedicated (hybrid) apps? If you want to go for apps then it's certainly a good deal to expose the data to work on via Web API which you can call from the client. For the website you generate the html via MVC which then calls the data from the client (browser).

    Grz, Kris.

    Monday, September 29, 2014 7:20 AM
  • User52625461 posted

    Hi,

    basically what most of them are saying is what I'm saying: don't call it from your MVC controller as it'll create a slowdown/bottleneck. I can understand you interpreted by Fuxiang as such but he's simply replying to how you can do what you ask. What lacks is good practice and well thought ways. In the end you want to eliminate all slow parts of your application or don't even create any in the first place.

    Ok. No issues. Just a simple question. Then why Wep Api option is provided in VS along with MVC application ?

    On that part of using the same infrastructure: do you want to create a responsive site to show in a mobile browser or create dedicated (hybrid) apps? If you want to go for apps then it's certainly a good deal to expose the data to work on via Web API which you can call from the client. For the website you generate the html via MVC which then calls the data from the client (browser).

    Grz, Kris.

    We are going to create dedicated (hybrid) app

    Monday, September 29, 2014 8:36 AM
  • User541108374 posted

    Hi,

    Then why Wep Api option is provided in VS along with MVC application ?

    Because were made separately during the years. They also come from different teams (ASP.NET and WCF team). If you follow up on MVC6 you'll notice that both will be "merged".

    Grz, Kris.

    Monday, September 29, 2014 8:54 AM