What's the difference between System.Web.Http and System.Web.Mvc?

10

I have a MVC project and I will have regular controls and controls providing services via webservices ( Controller and ApiController ).

And I do not know which one to use because I do not know which one is best.

    
asked by anonymous 25.06.2016 / 15:48

3 answers

5
  

What is the difference between System.Web.Http and System.Web.Mvc ?

Here we are talking about namespaces , and they serve different purposes.

The implementation of System.Web.Mvc is here . The implementation of System.Web.Http is here .

System.Web.Mvc implements, to a large extent, the ASP.NET MVC architecture, MVC5 being the latest version of this standard. It is being discontinued from new releases with the launch and stabilization of ASP.NET Core. Only bugs and some maintenance will be done, just like in the case of Web Forms.

System.Web.Http implements, to a large extent, the ASP.NET Web API architecture, being version 2.2 the last of this standard, also being discontinued with the launch of ASP.NET Core, which has as one of the objectives to unify the architectures.

  

I have a MVC project and I will have regular controls and controls providing services via webservices ( Controller and ApiController ).

Here we have some problems. Web Services have their own design pattern (the ASP.NET Web Service, script here ). What you want to do, as I understand it, is to serve some kind of data as a Web Service , but using either an MVC or Controller in> Web API.

  

And I do not know which one to use because I do not know which one is best.

Here it's worth talking about the differences about System.Web.Http.ApiController and about the System.Web.Mvc.Controller (which, I believe, is ultimately the purpose of the question).

Before, it's important to say that both can serve any kind of data. The difference is that Controller will do this using a ActionResult (that is, an abstract class that has several derivations, depending on what you want to serve), while ApiController will use a HttpResponseMessage .

The Controller implementation suggests facilities for working with HTML, JSON, Ajax, and traditional requests. In summary, the traditional internet sites. Less comprehensive and more specialized.

The implementation of ApiController suggests facilities to work with HTTP verbs (traditional browsers usually implement only GET and POST with some artifacts to implement the others), serialized objects and architecture without states, that is, the use session is discouraged ( although there are ways to do this ). In summary, made to work with mobile applications, integration with other sites, repository of information and files. More comprehensive and less specialized.

Conclusion

Since the idea is to build a service that works similar to a Web Service , ApiController is a more appropriate option (for both the Web API 2.2 and ASP.NET Core ). Controller can also be used to produce the same result, but will require a bit more work to compose and format the serialized element (such as XML, JSON, YAML, BSON, etc.).

Still about the Web API, the part of Media Formatters can be quite enlightening for its implementation.

    
29.06.2016 / 23:12
11

Has a more modern solution

If you're going to learn now, start a new project, and have nothing of a legacy, I'd advise using ASP.Net Core which is more modern. Forget these System.Web classes, any new technology model is more suitable for the current web template. Microsoft advises doing the same and will no longer evolve older versions (it will still be supported).

The legacy version is more mature, of course, but there are a lot of people using Core without problems, even in production . The rest is FUD . Evaluate what is best for you today and for the future.

Although at the time of this answer it may have some minor problems, it will be 100% soon, because the final 1.0 release is already released . And it's been a long time since Microsoft stopped stuffing things up. Follow the roadmap (the development of technology took 2 years). Some people have had problems in the past. But these things are transient. Here is an answer that goes from now on and not from the past.

In this new technology there is no distinction between Controller and ApiController eliminating confusion, only the former exists more flexibly.

You can still see the class in newer technology

a> and confirm how it is something better thought out and that there are gains in starting with it whenever possible. I will not go into details of 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 best to rely on non-transitory information.

Inheritance

These classes are abstract, so the only way to consume them is by inheriting them. Or you inherit from one or the other depending on your need.

Remembering that you can create controllers without inheriting from these classes, but this is the most practical and common way.

In more complex cases it is possible to inherit from these classes to another abstract class of yours that customizes the base controller to be used in the concrete controllers.

Differences between them

Of course there are projects that are worth staying in the older technology. In this case the main reason for choosing another class follows.

According to a SOen answer with 193 positive votes at the time of writing this answer, Controller is used when you want to generate views and ApiController is used when it will only generate a serialized data response (JSON is preferred) that will not render a page.

Although you can use it for something else, ApiController was not made for it , unless, of course, the data is HTML for a part of a page. This is enhanced by the official page from Microsoft . As it can, even ASP.Net MVC 5 was not ideal to send data to miscellaneous requests via Controller .

In old ASP.Net, the ApiController is used by the ASP.Net WebAPI to primarily service WebServices / RESTful API .

Example using MVC ending with delegation to view action completion:

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 WebAPI. To make requests here, you will usually use AJAX or some form of HTTP request in any type of application that you wish 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);
    }
}

If you're curious about what the abstract class code looks like, you can see your source .

    
25.06.2016 / 17:44
10

There is no such thing as "What's the best to use?" They have a different purpose.

If your project is in Asp.NET MVC, there is no need for ApiController , just as for 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 is not "is to be "both, so much that their purposes are different.

In summary, Asp.Net MVC focuses on return of Views , in other words, pages for the User, while Web APIs focus on returning REST services, in most cases, returning JSON. >

In Asp.NET Core that has 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 whoever accompanies the Microsoft Channel 9 < to> know it.

    
25.06.2016 / 18:48