11
I have an MVC project and in it I will have normal controls and controls providing services via webservices (Controller
and ApiController
).
And I don’t know which one to use because I don’t know which one is better.
11
I have an MVC project and in it I will have normal controls and controls providing services via webservices (Controller
and ApiController
).
And I don’t know which one to use because I don’t know which one is better.
12
This question was dated, today we can not question any of this, who encouraged the person to stay in the old version induced the person to error. See more in The . NET Framework is dead?.
If you will learn now, start a new project and have nothing legacy, I advise using the ASP.NET Core which is more modern. Forget these classes of System.Web
, any new technology model is more suitable for the current web model. Microsoft advises to do the same and will no longer evolve older versions (it will still be supported).
The legacy version is more mature, of course, but have a lot of people using Core without problems, even in production. The rest is FUD. Evaluate what’s best for you today and the future.
Although at the time of this answer he may have some small problems, he will be 100% soon, because the final version 1.0 is already released. And long ago Microsoft stopped releasing things full of problems. Follow the roadmap (the development of the technology took 2 years). Some people have had problems in the past. But these things are transitory. Here is an answer that goes forward and not to the past.
In this new technology there is no distinction between Controller
and ApiController
eliminating confusion, only the former exists more flexibly.
Yet can see the class in the newest technology and confirm that it is something better thought out and that there are gains in starting with it whenever possible. I will not go into detail about the advantages of ASP.NET Core because it is not the focus of the question. The disadvantage for those who have no legacy is transient, and even questionable at the present time. Beware of outdated information, it is better to trust in what is not transitory.
These classes are abstract, so the only way to consume them is to inherit from them. Either you inherit from one or the other, depending on your need.
Remembering that it is possible to create controllers without inheriting these classes, but this is the most practical and common form.
In more complex cases it is possible to inherit these classes to another abstract class of yours that customizes the base controller to be used in concrete controllers.
Of course there are projects that are worth staying in the older technology. In this case follows the main reason to choose another class.
According to a reply in Soen with 193 votes in favour at the time I write this reply, Controller
is used when you want to generate views and the ApiController
is used when it will only generate a serialized data response (JSON is preferred) that will not render a page.
Although I might use it for something else, the ApiController
wasn’t made for this, unless, of course, the data is HTML for a part of a page. This is reinforced by Microsoft’s official website. As it can, even ASP.Net MVC 5 was not ideal to send data for various requests through Controller
.
In the old ASP.Net to ApiController
is used by ASP.Net Webapi to serve mainly Webservices/Restful API.
Example using the MVC ending with the delegation to the vision to complete the action:
public class MoviesController : Controller {
private MovieDBContext db = new MovieDBContext(); //pegar do model
// GET: /Movies/
public ActionResult Index() {
return View(db.Movies.ToList()); //note a delegação para a view
}
}
You can see the source code of the abstract class.
Example using the Webapi. To make requests here usually will be used AJAX or some form of request via HTTP in any type of application you want to consume your webservice.
public class ProductsController : ApiController {
Product[] products = new Product[] {
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts() {
return products;
}
public IHttpActionResult GetProduct(int id) {
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null) {
return NotFound();
}
return Ok(product);
}
}
I put in the Github for future reference.
If you’re curious about the abstract class code, it is possible to see its source.
ASP.NET Core is not yet stable. It has several problems at various stages of development. It is not yet time to recommend it.
But it is a matter of days, before someone can finish an important project started now it will already be ok, because it is almost ok. The final version should be released this week (or something like that) and even if she has small problems, they will fix it. There are many people using it successfully. Not recommending will make the answer outdated. The answers here need to last. I find it counterproductive to recommend something old that will become legacy except in very specific cases that would be determined by the question. At least soon you can return and change the negative response.
@I’m sorry, but I can’t agree with you. I think my answer is safe for a minimum of 2 years. Asp.NET Core is an excellent change, and I’m more eager than you to stay stable. However, it is not enough for Microsoft to say that something is ready. Only those who have already experienced a bug in producing that was caused by updates know what I’m talking about. Your system stop and you depend on something to be able to fix. That’s why I think your answer is focused on the wrong place. AP asked the question today, that is, with current technologies.
However, his answer gives to understand that he must migrate to Asp.NET Core, which is not true. What the manufacturer says about something new is always positive, what the community that uses it talks about, not always.
@Randradedo you mean that Microsoft is wrong? Are the people who are successfully using and even producing me wrong? Will it take two years for the product to become stable? On today’s date the product is as full of bugs as it was months ago? Is it better to rely on outdated information? The fact that you trust the product and cannot use it does not mean that it is Rui,. The fact that it is not suitable for some, does not mean that it is not for the AP.
10
There’s no such thing as "Which is the best to use?" They have different purpose.
If your project is in Asp.NET MVC, there is no need to Apicontroller, in the same way that if it is Web API, there is no need to have MVC Controller.
Now, if your project uses both, separate the two. C#
does not have multiple inheritance, which indicates that your Controller not "to be" both, so much so that their purposes are distinct.
In summary, Asp.Net MVC focuses on return of Views
, in other words, pages for the User, while Web Apihas focus on returning REST services, in most of its cases returning JSON.
In the Asp.NET Core this changed, and the same controller can return both. However, this version is not stable and has some bugs, so much so that it is only used by "enthusiasts", and who accompanies the Channel 9 by Microsoft you know that.
Randrade, I didn’t understand this "separate the two"... how would I separate? Type I will have an area for users of the site, and another that provides data for other applications all in the same context, on the same server, using token authentication...
@diegocolli Simple, The controllers relating to the Web API you inherit from Apicontroller, the others of Controller. The same controller should not do both.
6
What’s the difference between
System.Web.Http
andSystem.Web.Mvc
?
Here we are talking about namespaces, and serving different purposes.
The implementation of System.Web.Mvc
is here. The implementation of System.Web.Http
is here.
System.Web.Mvc
implements, in large part, the ASP.NET MVC architecture, being the MVC5 the latest version of this standard. It is discontinued from new releases with the release and stabilization of ASP.NET Core. Only bugs and some maintenance will be done, as in the case of Web Forms.
System.Web.Http
implements, in large part, the ASP.NET Web API architecture, being the latest version of this standard, also being discontinued with the release of ASP.NET Core, which aims to unify architectures.
I have an MVC project and in it I will have normal controls and controls providing services via webservices (
Controller
andApiController
).
Here we have some problems. Web Services have their own design standard (ASP.NET Web Service, script here). What you want to do, from what I understand, is serve some kind of data as a Web Service, but using either a Controller MVC or a Controller Web API.
And I don’t know which one to use because I don’t know which one is better.
Here it is worth talking about the differences about the System.Web.Http.ApiController
and on the System.Web.Mvc.Controller
(which, I believe, is after all the purpose of the question).
Rather, it is important to say that both can serve any kind of data. The difference is that the Controller
will do it using a ActionResult
(that is, an abstract class that has several derivations, depending on what it wants to serve), while the ApiController
will use a HttpResponseMessage
.
The implementation of Controller
suggests facilities to work with HTML, JSON, Ajax and traditional requests. In short, the traditional websites. Less comprehensive and more specialized.
The implementation of ApiController
suggests facilities for working with HTTP verbs (traditional browsers usually implement only GET
and POST
with some artifices to implement the others), serialized objects and architecture without states, that is, the use of session is discouraged (even though there are ways of it being done). In short, made to work with mobile applications, integration with other sites, information repository and files. More comprehensive and less specialized.
whereas the idea is to set up a service that works in a similar way to a Web Service, ApiController
is a more suitable option (for both Web API 2.2 and ASP.NET Core). Controller
can also be used to produce the same result, but it will require a little more work to compose and format the serialized element (such as XML, JSON, YAML, BSON, etc.).
Still on Web API, the part of Media Formatters can be quite enlightening for its implementation.
Browser other questions tagged c# asp.net-mvc .net asp.net-web-api
You are not signed in. Login or sign up in order to post.
Both are outdated, don’t use them.
– Maniero
Should or should not extend Controller and Apicontroller?
– Andre
It must. It is the only way to consume it since it is abstract.
– Maniero