Skip to content

Cheap Windows Hosting ASP.NET

ASP.NET Hosting Cheap | Faster

X
  • Hosting Award
  • Hosting Comparison
  • Hosting Review
  • Tutorial
  • Contact
Menu

ASP.NET MVC 6 Tutorial :: Migrate ASP.NET Web API 2 To MVC 6

Posted on June 2, 2016June 16, 2016 by Mary Bacine

If you want to create a new MVC 6 project from the default starter template, it will contain the following code in the Startup class, under ConfigureServices method:

ASP.NET MVC 6 Tutorial :: Migrate ASP.NET Web API 2 To MVC 6

// Uncomment the following line to add Web API servcies which makes it easier to port Web API 2 controllers.
// You need to add Microsoft.AspNet.Mvc.WebApiCompatShim package to project.json
// services.AddWebApiConventions();

This pretty much explains it all – the Compatibility Shim is included in an external package, Microsoft.AspNet.Mvc.WebApiCompatShim and by default is switched off for new MVC projects. Once added and enabled, you can also have a look at the UseMvc method, under Configure. This is where central Web API routes can be defined:

app.UseMvc(routes =>
{
routes.MapRoute(
name: “default”,
template: “{controller}/{action}/{id?}”,
defaults: new { controller = “Home”, action = “Index” });
// Uncomment the following line to add a route for porting Web API 2 controllers.
// routes.MapWebApiRoute(“DefaultApi”, “api/{controller}/{id?}”);
});

Inheriting from ApiController

Since the base class for Web API controllers was not Controller but ApiController, the shim introduces a type of the same name into MVC 6.

While it is obviously not 100% identical to the ApiController from Web API, it contains the majority of public proeprties and methods that you might have gotten used to – the Request property, the User property or a bunch of IHttpActionResult helpers.

Returning HttpResponseMessage

The shim introduces the ability to work with HttpResponseMessage in MVC 6 projects. How is this achieved? First of all, the Microsoft.AspNet.WebApi.Client package is referenced, and that brings in the familiar types – HttpResponseMessage and HttpRequestMessage.

On top of that, an extra formatter is injected into your application – HttpResponseMessageOutputFormatter. This allows you to return HttpResponseMessage from your actions, just like you were used to doing in Web API projects!

How does it work under the hood? Remember, in Web API, returning an instance of HttpResponseMessage bypassed content negotiation and simply forwarded the instance all the way to the hosting layer, which was responsible to convert it to a response that was relevant for a given host.

In the case of MVC 6, the new formatter will grab your HttpResponseMessage and copy its headers and contents onto the Microsoft.AspNet.Http.HttpResponse which is the new abstraction for HTTP response in ASP.NET 5.

As a result such type of an action as the one shown below, is possible in MVC 6, and as a consequence it should be much simpler to migrate your Web API 2 projects.

public HttpResponseMessage Post()
{
return new HttpResponseMessage(HttpSattusCode.NoContent);
}

Binding HttpRequestMessage

In Web API it was possible to bind HttpRequestMessage in your actions. For example this was easily doable:

[Route(“test/{id:int}”)]
public string Get(int id, HttpRequestMessage req)
{
return id + ” ” + req.RequestUri;
}
[Route(“testA”)]
public async Task<TestItem> Post(HttpRequestMessage req)
{
return await req.Content.ReadAsAsync<TestItem>();
}

The shim introduces an HttpRequestMessageModelBinder which allows the same thing to be done under MVC 6. As a result, if you relied on HttpRequestMessage binding in Web API, your code will migrate to MVC 6 fine.

How does it work? The shim will use an intermediary type, HttpRequestMessageFeature, to create an instance of HttpRequestMessage from the ASP.NET 5 HttpContext.

HttpRequestMessage extensions

Since it was very common in the Web API world to use HttpResponseMessage as an action return type, there was a need for a mechanism that allowed easy creation of its instances. This was typically achieved by using the extension methods on the HttpRequestMessage, as they would perform content negotiation for you.

HttpError

If you use/used the CreateErrorResponse method mentioned above, you will end up relying on the HttpError class which is another ghost of the Web API past rejuvenated by the compatibility shim.

HttpError was traditionally used by Web API to serve up error information to the client in a (kind of) standardized way. It contained properties such as ModelState, MessageDetail or StackTrace.

It was used by not just the CreateErrorResponse extension method but also by a bunch of IHttpActionResults – InvalidModelStateResult, ExceptionResult and BadRequestErrorMessageResult. As a result, HttpError is back to facilitate all of these types.

Posted in ASP.NET, ASP.NET MVC, Hosting TutorialTagged affordable asp.net mvc 6 shared hosting, ASP.NET MVC 6, asp.net mvc 6 hosting, asp.net mvc 6 hosting india, asp.net mvc 6 hosting palestina, asp.net mvc 6 hosting poland, asp.net mvc 6 hosting south africa, asp.net mvc 6 hosting turkey, asp.net mvc 6 tutorial, asp.net mvc hosting, austria asp.net mvc 6 hosting, belgium asp.net mvc 6 hosting, cheap asp.net mvc 6, cheap asp.net mvc 6 hosting, cheap europe asp.net mvc 6 hosting, europe asp.net mvc 6 hosting, excellent asp.net mvc 6 hosting, free and best asp.net mvc 6 hosting, free and recommended asp.net mvc 6 hosting, full trust asp.net hosting, germany asp.net mvc 6 hosting, greece asp.net mvc 6 hosting, italy asp.net mvc 6, low cost asp.net mvc 6 hosting, most recommended asp.net hosting, MVC 6 Tutorial, netherland asp.net mvc 6 hosting, optimum asp.net mvc 6 hosting, powerfull asp.net mvc 6 hosting, recommended windows asp.net hosting, reliable asp.net mvc 6 hosting, reliable free asp.net mvc 6 hosting, secure windows hosting, spain asp.net mvc 6 hosting, sweden asp.net mvc 6 hosting, top asp.net mvc 6 hosting, uk asp.net mvc 6 hosting, unlimited free windows hosting

Post navigation

SQL 2016 Reporting Services Hosting, SSRS hosting at HostForLIFE.eu
Reliable ASP.NET Core 1.0 RC2 Hosting – HostForLIFE.eu

Related Post

  • Best Cheap ASP.NET MVC 6 Hosting in Europe
  • eNom VS GoDaddy – Cheap ASP.NET Hosting Comparison
  • Benefits of Social Marketing
  • Cheap and Best Windows ASP.NET Hosting :: HostForLIFE.eu VS VidaHost
  • Best and Cheap ASP.NET MVC 5 Hosting Service
  • ASP.NET MVC 6 Tutorial :: Handle Multiple Submit Buttons On The Same Form in MVC 6
  • Best and Cheap IIS 8.5 Hosting Provider
  • Best and Cheap Windows Server 2012 Hosting – HostForLIFE.eu
  • Affordable ASP.NET Core 1.0 RC2 Hosting – Cheap Windows Hosting ASP.NET

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Best SQL 2016 Hosting

Categories

  • ASP.NET
  • ASP.NET Ajax
  • ASP.NET MVC
  • Entity Framework
  • Hosting Comparison
  • Hosting Review
  • Hosting Tutorial
  • IIS
  • Joomla
  • nopCommerce
  • Silverlight
  • SQL Server

Recent Posts

  • A2 Hosting vs ASPHostPortal ASP.NET Hosting – Who is Your Favourite?
  • Looking for A2 ASP.NET Hosting Alternatives?
  • Windows Dedicated Server with HostForLIFE.eu. Is it Good?
  • Let’s Move From Ionos ASP.NET Hosting
  • Choosing Your Best ASP.NET Core Hosting Provider: ASPHostPortal.com VS GoDaddy in Comparison

Our Partner

  • Best ASP.NET Hosting
  • Best Windows Hosting
  • Cloud ASP.NET Hosting
  • ECommerce Hosting Review
  • Recommended ASP.NET
  • Reliable Windows Hosting
  • Review ASP.NET Hosting
  • Windows Hosting Bulletin
  • Windows Hosting Leaders
  • Windows ASP.NET Hosting
  • Windows Hosting Review
  • Best Cheap Hosting ASP.NET
  • Hosting Review ASP.NET
  • Full Trust Hosting ASP.NET
  • Cheap Australia ASP.NET Hosting
  • Review Core ASP Hosting
  • Cheap Australia ASP.NET Hosting
  • Best Cloud ASP.NET Hosting
  • Creative Videos
  • Business Vendor
  • Reliable ASP.NET Hosting
  • Cheap ASP.NET Hosting Review
  • European ASP.NET Hosting
  • UK ASP.NET Hosting
  • ASP.NET Hosting Review
  • India ASP.NET Hosting
  • Best India ASP.NET Hosting
  • Cheap ASP.NET Hosting Review
  • Best ASP.NET Hosting Review
  • Easy Hosting ASP.NET
  • Cheap Windows Hosting
  • Reliable ASP.NET Hosting Review
Created by cheapwindowshostingasp.net
  • Hosting Award
  • Hosting Comparison
  • Hosting Review
  • Tutorial
  • Contact