Is it possible to add ODataController controller to an MVC web app? - asp.net-mvc

I have an existing asp.net MVC 5 web application. I do not want to crate a separate project for asp.net webapi OData and I want re-use www domain of the web app for OData app. I'd like to add some ODataControllers to that MVC web application to be consumed in some cshttml view pages with flexible, robust queries. Is it possible to do that? If so, how is about the routing configs must be updated for web app routes and OData routes? Thank you.

Related

MVC Architecture in .Net6

Basically I want to follow MVC Architecture. So should I use Asp.Net Web Api or Asp.Net MVC for backend development?
Does Asp.Net Web Api have MVC architecture? If does help me build Web Api using MVC Architecture in .net 6.
Based on what you've written, you have a couple options.
MVC is a front-end design pattern. For backend development, you could create a Web API project, which would give you the ability to create models and controllers similar to MVC. You would not create views with Web API as there is no UI.
Alternatively, and especially if it's a smaller project where scalability is not a concern, you could create an MVC project representing the whole app and include the backend code there rather than creating a separate backend project. In this case you'd have models and controllers driving the UI, but not the backend.

How to integrate WebAPI to an MVC application

I am developing an MVC5 application and use Entity Framewerok 6 code first on this. Now we we will also develop an android application that will interact with the MVC application (CRUD operations) by using the web services. At this stage I want to be clarified about the issues below:
1) I think WebAPI is better option for us as we use the services on android apps. What do you suggest?
2) In order to integrate WebAPI to an MVC project, which changes should be made? On the other hand, can we use the same controller and data layer methods (i.e. SaveChanges, etc.) by making some modifications i.e. inheritance? Or do we have to create a seperate methods for web services? Could you give an example by code?
3) Does integrating WebAPI to the MVC project affect the MVC project's abilities or methods? I mean that is there any disadvantage integrating WebAPI to an MVC project?
Any help would be appreciated.
1) That's a good idea. Web API is easy to implement and consume
2) You don't need to make changes to intergate Web API in your application: just start using it. As you want to expose CRUD operations from EF a good idea would be to implement ODATA services. Or use something like Breeze (depending on how you want to consume the services). See "MVC and Web API" bwelow
3) Web API doesn't affect at all the MVC part, unless you make a mistake setting the routes. Although they run in the same host, they work completely independent of each other.
MVC and Web API
Unless you need to do something special, like exposing Web API in a different URL or "domain name", MVC and Web API are implemented in the same web application project. To start using Web API in your MVC project simply add a new controller. Perhaps you'll have to include also the WEB API route configuration, and some other Web API configuration.
If you want to expose the EF model throug Web API you simply have to follow the instructions in the link to create an ODATA controller, which will expose the EF model as a RESTful service, allowing you to execute the CRUD operations to the EF model through URLs.
NOTE: What you want to do is a very frequesnt pattern in MVC applications: MVC is used for generating the views, and Web API fos exposing functionalities that can be easily consumed from the views usin Javascript + AJAX. Don't be afraid to use it. You'll find no problems at all

Integrating ASP.Net Web API With MVC

how to create a web application using the ASP.NET Web API 2 project template and do some data operations like reading the data from the database and calling the Web API Controller method in the MVC Controller and call the adjacent view? any resources for that? I found one but it doesn;t go into much detail
http://www.c-sharpcorner.com/UploadFile/4b0136/integrating-Asp-Net-web-api-with-mvc-basic-infrastructure-by/
The below example provides a detailed explanation on how to call WebApi from a C# app, you can follow same for the asp.net mvc application also
http://www.asp.net/web-api/overview/advanced/calling-a-web-api-from-a-net-client

Why does every new asp.net mvc 4 project template base on WebApi Controller

When I create a new ASP.NET MVC 4.0 project e.g. Single Page App or Mobile etc... they all have Controller classes inheriting from ApiController.
I do not need to expose a web service to someone else. I just want to run a public website with a private webapplication if logged in. I do not want ApiController but I want a Single Page App.
Why have they done it that way?
WebApi does not necessarily imply that you are creating a web service for someone else. Instead, when you are developing a single page app, you would use the WebApi controllers to deal with getting/posting data via ajax.
There is nothing stopping you from using regular controllers, but the WebApi is well suited for SPA. See any of the online tutorials where this technique is used.
It sounds like you want to create an Mvc4 Web Application using the Internet Application project template. This template uses forms authentication and creates controllers that inherit from System.Web.Mvc.Controller. If you are using Visual Studio 2012 then this template is installed (along with a handful of others including the Api Web template).
Right click on your controllers folder -> Add -> Controller and then choose an MVC controller from the template drop down. You don't have to use an API Controller.
If you think of what a SPA is, it's essentially an HTML page that uses JavaScript to get data from a WebAPI or some other web service. Like Bort said, web API calls are very well suited for SPA.
Personally, for a single page app, before they added the SPA template in Update 1 I'd just create a static .html page and make RESTful calls into my WebAPI controllers.

Which's the best performance between Asp.net MVC controller VS. Wcf for Silverlight Application?

I found that Asp.net Mvc controller can serve both Asp.net Mvc View and Silverlight application via DynamicActionResult(It can be Simple Action Result or Json Action Result depend on request type). So, I have 3 options for creating middle-tier for Silverlight application.
Pure WCF Service Every request to WCF must be declare in interface. So, it's strongly-typed connection.
Asp.net MVC Controller It can serve both Asp.net MVC View and Silverlight application at the same time.
Using both of them I found that it's possible to doing this by using the answer in the following link. I think, It doesn't good idea for creating both of them.
WCF Service with Asp.net MVC application
Which's the best performance between WCF Service and Asp.net MVC Controller?
Thanks,
Do you have the kind of service that would benefit from caching? If so, my testing says that MVC with Output Caching turned on is very much faster than WCF.

Resources