Is it possible to use/call my Web API methods inside a MVC controller?
I have a Web Api to use in mobile and others plattaforms and I´m developing a .NET Mvc Website.
Does that architecture makes sense?
Thanks
Yes it's possible although if you're expecting to consume your API from a number of different clients I would suggest you create your API as a separate application that can then be managed/scaled accordingly.
Essentially you are referring to "dog-fooding" your own API, making your own web application no different to any other client.
We do something similar and have our MVC application call our API (using HttpClient). We also have a lot of client side code within the same application that calls the API directly using CORS.
We've had this architecture running in production for 2 years now without any issue.
Here is How I call my Web API controller from MVC controller:
public class invoiceController : ApiController
{
private myEntities db = new db1Entities();
// GET api/invoices
public IQueryable<invoices> Getinvoices()
{
return db.invoices;
}
}
inside separate MVC controller:
public ActionResult ShowInvoices()
{
var webApi = new invoicesController();
//this must return strongly typed object
var myarray = webApi.Getinvoices();
return View(myarray);
}
Related
I'm using ASP.NET Core MVC and Web API and trying to consume the internal Web API (done using ApiController, prepare for future cross-platform application use), I saw an answer which doesn't need to use HttpClient or any Http Request features to get data from Web API, refer: Consuming web api controller action from mvc controller
I'm trying to do the similar thing, but my auto generated Web API Controller comes with DBContext parameter, which causing me unable to follow what is mentioned from the link.
Below is what i have in my Controller which caused me unable to follow actions mentioned in the link above:
private readonly MyTestDBContext _context;
public MfgProductsController(MyTestDBContext context)
{
_context = context;
}
If the "MyTestDBContext context" parameter supposed to remain, what should I write in my Controller in order to pass the DBContext in?
Or if there's a way to remove "MyTestDBContext context" from the parameter, how the constructor supposed to change to?
Let the container do its job, just add the controller as a dependency in your controller:
public class MyController : Controller
{
public MyController(MfgProductsController productsController)
{
_productsController = productsController;
}
private readonly MfgProductsController _productsController;
}
It should fill all dependencies for you.
If I build a Web API Project in Asp.Net Core 1.x, and I want to build the front-end in the same Project or Solution, is it possible to create front-end pages and use Razor with Visual Studio's Intellisense?
The application is built around an API for public consumption, but since my application will use the same data, I thought it would make sense to consume my own API instead of building separate methods or constructs for API calls vs "regular" MVC (call the controller, gets the model data, return the view). A client will have their own front-end and get the data. I will have my own front-end, but I want it integrated in the same VS Solution.
One downside is I lose my Intellisense because I am building it around consuming the JSON returning from the API. I understand that an API is about returning data, not Views. I'm trying to get the best of all worlds and be more productive with Visual Studio features.
All I have read is older. SO has older question as well. I read a a lot about returning a View with an API, but I'm not sure I want that. I want to do like a normal non-Web API project and API project at the same time.
Is this possible?
My understanding is no/sort of because the whole point of Razor is that it is done on the server and the API is for clients outside your application. In other words, I'd need to build a controller that called my API controllers, so I get Intellisense which seems a bit redundant and harder work on the server.
Some possibilities:
Building REST APIs using ASP.NET Core with Razor syntax
ASP.NEt MVC using Web API to return a Razor view
asp.net mvc consuming asp.net web api end point
EDIT: This seems to be logical, https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas
EDIT: Here is what I did, using this as a guide:
https://msdn.microsoft.com/en-us/magazine/mt763233.aspx
I created an Area in my Project. Under this I created the name of an Area and under this I created Controllers, Views (and under this Home).
In my Startup.cs file I added,
app.UseMvc(routes =>
{
routes.MapRoute(name: "areaRoute",
template: "{area:exists}/{controller=Home}/{action=Index}");
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}");
});
The URL is localhost:port/AreaName
From within my Controller, I was able to reuse the services from my Service Layer, an abstraction I had decided on earlier in the project.
namespace books.Areas.Controllers
{
[Area("Books")]
public class HomeController : Controller
{
private readonly AppSettings _appSettings;
public HomeController(Microsoft.Extensions.Options.IOptions<AppSettings> appSettings)
{
_appSettings = appSettings.Value;
}
// GET: /<controller>/
public IActionResult Index()
{
var myConn = _appSettings.ConnectionString;
var getBooks = new BookLoanService(myConn);
return View(getBooks.GetAll());
}
}
}
This is the same Service Layer my API controller uses. The Service Layer has (I am using Dapper),
public List<books> GetAll()
{
using (IDbConnection db = new SqlConnection(_connectionString))
{
string SqlString = "SELECT TOP 5 last_name, first_name FROM books";
var myBooks = (List<books>)db.Query<books>(SqlString);
return myBooks ;
}
}
Once I was able to get my types, I was able to use Razor in my Index in my Area:
#model System.Collections.Generic.List<books>
#{
}
<html>
<body>
#foreach(var person in Model)
{
<ul><li>#person.last_name</li></ul>
}
</body>
</html>
Without modification, I am still able to use my api/values type of URL to get to my API, and I will be able to refresh part of my page after that initial server rendering by calling the API via Ajax.
I am developing project in MVC with normal controller and Web API 2 controller.
I am using MVC 4 with Razor, Normal Controller for Website, WebAPI controller that will work as REST service for Iphone device or android device.i want to use normal MVC normal controller to take advantages of data annotations(required, regularexpression etc in Model) thats why i am using both API controller and Normal controller.
I am writing my all DB related operation ( Fetch, insert, update, delete) in WebAPI. so that same method can be used by Website request and Mobile device request.
To access the method of WebAPI , i am creating the object like this.
just like this.
WEBAPI Method
public class AccountAPIController : ApiController
{
[AcceptVerbs("Get", "Post"), ActionName("GetSingle")]
public UserProfile GetSingle(string email)
{
// return userpofile;
}
}
NORMAL MVC Controller
public class AccountController : Controller
{
[HttpPost]
public ActionResult Getsingle(string email)
{
AccountAPIController API = new AccountAPIController();
var data= API.GetSingle(email);
return View(data);
}
is this right way to consume WebAPI? please suggest the best way to consume web api both by website and Iphone or android device.
I design a simple online store. It has a product card, a list of products, the ability to add comments to the product, user registration, search products by price and other criteria. Maybe online store will have mobile clients (android and ios).
I want to try to work with RESTful. Is it normal to create this store, using RESTful? If it is normal, then I have a few questions.
When I create the usual sites, I write the following code:
public ActionResult Index()
{
var products = this.productRepository.GetAll();
return View(products);
}
How to change the architecture of the site, if I use RESTful. What must this method sent to client? Html only? And then the client has to execute AJAX request to the api to get the data?
RESTful web services is an HTTP-based services, any HTTP related applications can implement it by using WebApi,WCF, etc.
To build the online store project, of course you can use WebApi to build RESTful web services.
It's hard to say if using RESTful web services is normal or not, you can build an web application without building RESTful web services.
It only depends on your needs and preferences.
Let's say you're using WebApi to build RESTful web services for your project.
below is an example showing how you can implement it.
WebApi Controller
public class ProductsController : ApiController
{
public IEnumerable<Product> GetAllProducts()
{
return this.productRepository.GetAll();
}
}
MVC Action
Example 1: Calling Web API controller actions from MVC action
public ActionResult Index()
{
var webApi = new ProductsController();
return View(webApi.GetAllProducts());
}
Example 2: Populate all products in the view by calling Web API using Ajax
View:
<div id="contents"></div>
JS file
$(function() {
showAllProducts();
function showAllProducts() {
var url = "http://localhost:13131/api/Products/";
$.getJSON(url, function(result) {
$("#contents").append(result); // here you need do more than this.
});
}
});
What way to implement Web API depends on your need, there are a lot of way to implement it.
For your project, it's good to try different methods, you can learn more about it in the process.
Hope it helps.
I've been playing around with ASP.NET MVC 4 beta and I see two types of controllers now: ApiController and Controller.
I'm little confused at what situations I can choose a particular controller.
For ex: If I want to return a view then I've to use ApiController or the ordinary Controller? I'm aware that the WCF Web API is now integrated with MVC.
Since now we can use both controllers can somebody please point at which situations to go for the corresponding controller.
Use Controller to render your normal views. ApiController action only return data that is serialized and sent to the client.
here is the link
Quote:
Note If you have worked with ASP.NET MVC, then you are already familiar with controllers. They work similarly in Web API, but controllers in Web API derive from the ApiController class instead of Controller class. The first major difference you will notice is that actions on Web API controllers do not return views, they return data.
ApiControllers are specialized in returning data. For example, they take care of transparently serializing the data into the format requested by the client. Also, they follow a different routing scheme by default (as in: mapping URLs to actions), providing a REST-ful API by convention.
You could probably do anything using a Controller instead of an ApiController with the some(?) manual coding. In the end, both controllers build upon the ASP.NET foundation. But having a REST-ful API is such a common requirement today that WebAPI was created to simplify the implementation of a such an API.
It's fairly simple to decide between the two: if you're writing an HTML based web/internet/intranet application - maybe with the occasional AJAX call returning json here and there - stick with MVC/Controller. If you want to provide a data driven/REST-ful interface to a system, go with WebAPI. You can combine both, of course, having an ApiController cater AJAX calls from an MVC page.
To give a real world example: I'm currently working with an ERP system that provides a REST-ful API to its entities. For this API, WebAPI would be a good candidate. At the same time, the ERP system provides a highly AJAX-ified web application that you can use to create queries for the REST-ful API. The web application itself could be implemented as an MVC application, making use of the WebAPI to fetch meta-data etc.
Which would you rather write and maintain?
ASP.NET MVC
public class TweetsController : Controller {
// GET: /Tweets/
[HttpGet]
public ActionResult Index() {
return Json(Twitter.GetTweets(), JsonRequestBehavior.AllowGet);
}
}
ASP.NET Web API
public class TweetsController : ApiController {
// GET: /Api/Tweets/
public List<Tweet> Get() {
return Twitter.GetTweets();
}
}
I love the fact that ASP.NET Core's MVC6 merged the two patterns into one because I often need to support both worlds. While it's true that you can tweak any standard MVC Controller (and/or develop your own ActionResult classes) to act & behave just like an ApiController, it can be very hard to maintain and to test: on top of that, having Controllers methods returning ActionResult mixed with others returning raw/serialized/IHttpActionResult data can be very confusing from a developer perspective, expecially if you're not working alone and need to bring other developers to speed with that hybrid approach.
The best technique I've come so far to minimize that issue in ASP.NET non-Core web applications is to import (and properly configure) the Web API package into the MVC-based Web Application, so I can have the best of both worlds: Controllers for Views, ApiControllers for data.
In order to do that, you need to do the following:
Install the following Web API packages using NuGet: Microsoft.AspNet.WebApi.Core and Microsoft.AspNet.WebApi.WebHost.
Add one or more ApiControllers to your /Controllers/ folder.
Add the following WebApiConfig.cs file to your /App_Config/ folder:
using System.Web.Http;
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Finally, you'll need to register the above class to your Startup class (either Startup.cs or Global.asax.cs, depending if you're using OWIN Startup template or not).
Startup.cs
public void Configuration(IAppBuilder app)
{
// Register Web API routing support before anything else
GlobalConfiguration.Configure(WebApiConfig.Register);
// The rest of your file goes there
// ...
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
ConfigureAuth(app);
// ...
}
Global.asax.cs
protected void Application_Start()
{
// Register Web API routing support before anything else
GlobalConfiguration.Configure(WebApiConfig.Register);
// The rest of your file goes there
// ...
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
// ...
}
This approach - together with its pros and cons - is further explained in this post I wrote on my blog.
Quick n Short Answer
If you want to return a view, you should be in "Controller".
Normal Controller - ASP.NET MVC: you deal with normal "Controller" if you are in ASP.net Web Application. You can create Controller-Actions and you can return Views().
ApiController Controller: you create ApiControllers when you are developing ASP.net REST APIs. you can't return Views (though you can return Json/Data for HTML as string). These apis are considered as backend and their functions are called to return data not the view
More Details here
Every method in Web API will return data (JSON) without serialization.
However, in order to return JSON Data in MVC controllers, we will set the returned Action Result type to JsonResult and call the Json method on our object to ensure it is packaged in JSON.
The main difference is: Web API is a service for any client, any devices, and MVC Controller only serve its client. The same because it is MVC platform.
If you create a new web application in latest framework 4.7.2 you will both of them to be configured by default and can build you application on that
In Asp.net Core 3+ Vesrion
Controller: If wants to return anything related to IActionResult & Data also, go for Controllercontroller
ApiController: Used as attribute/notation in API controller. That inherits ControllerBase Class
ControllerBase: If wants to return data only go for ControllerBase class