How to Migrating From ASP.NET Web API 2 to MVC 6

How to Migrating From ASP.NET Web API 2 to MVC 6

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC 6 hosting. If you create a new MVC 6 project from the default starter template, it will contain the following code in the Startup class, under ConfigureServices method:

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:

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.

Binding HttpRequestMessage

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

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.

How to Create Nested WebGrid with Expand/Collapse in ASP.NET MVC 6

How to Create Nested WebGrid with Expand/Collapse in ASP.NET MVC 6

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC 6 hosting. In this post, I am explain How to Create Nested WebGrid with Expand/Collapse in ASP.NET MVC 6.
Steps :

aspnetmvclogo

Step – 1 : Create New Project.

  • Go to File > New > Project > Select asp.net MVC6 web application > Entry Application Name > Click OK > Select Internet Application > Select view engine Razor > OK

Step-2: Add a Database.

  • Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

  • Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > OK.

In this example, I have used two tables as below

Step-4: Add Entity Data Model.

  • Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
  • A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
  • Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a class for create a view model.

  • 1st : Add a folder.
  • Go to Solution Explorer > Right Click on the project > add > new folder.
  • 2nd : Add a class on that folder
  • Go to Solution Explorer > Right Click on that folder > Add > Class… > Enter Class name > Add.

Write the following code in this class

Step-6: Add a new Controller.

  • Go to Solution Explorer > Right Click on Controllers folder form Solution Explorer > Add > Controller > Enter Controller name > Select Templete “empty MVC Controller”> Add.

Step-7: Add new action into your controller for show nested data in a webgrid.

Here I have added “List” Action into “Order” Controller. Please write this following code

Step-8: Add view for the Action & design.

  • Right Click on Action Method (here right click on form action) > Add View… > Enter View Name > Select View Engine (Razor) > Check “Create a strong-typed view” > Select your model class > Add.

NOTE ” Please Rebuild solution before add view

Cheap ASP.NET MVC 6 Hosting only 1$/mo Click here

Save

Cheap ASP.NET MVC 6 Hosting with Rich Features and Affordable Plans

Cheap ASP.NET MVC 6 Hosting with Rich Features and Affordable Plans

CheapASPNETHostingReview.com | Best and cheap ASP.NET MVC 6 hosting. Find the best ASP.NET MVC 6 Web Hosting company from our recommended list of good asp.net web hosts below. These hosts offer good asp.net hosting plans and are feature rich.

As many webmasters ask about where and how to choose one of the best ASP.NET hosting provider. In this post, we display the top 3 providers, including ASPHostPortal, HostForLIFE and DiscountService, which enjoy high reputation in this field.

NoWeb HostingFeatureBottom LinePrice
1

asphostportal-icon-e1421832425840-120x120-e1424663413602

  • Unlimited Sites
  • 30 Day Money Back
  • 24/7/365 Tech Support
  • Scale-Up-Anytime

ASPHostPortal offers cheap domain hosting
Excellent customer support
and an impressive array
of supplementary features.
Very easy to use

$5.00/Mo
2hostforlife-icon-e1421832276583-120x120-e1424663388212
  • Everyting Unlimited
  • No Hidden Fees
  • Domain Included!
  • 24/7 Tech Support
HostForLIFE is a popular web hosting service
with dependable customer support,
Hundreds of customizable designs,
Unlimited bandwidth, and
WordPress install.
€3.00/Mo
3

discountservice-icon-e1421396726386-120x120-e1424663401956

  • 100% Network Uptime
  • Instant Setup!
  • 30 Days Money Back
  • 24/7 Tech Support
DiscountService offers a cheap hosting
and easy to use web builder
with hundreds of templates,
dozens of apps, and
24/7 customer support
$7.00/Mo

ASPHostPortal.com – Cost-effective ASP.NET Hosting Service Provider

ASPHostPortal is the leading company in ASP.NET hosting industry, supports the latest cutting-edge Microsoft technology. The service of this company starts from $5.00/mo regularly.

Though the price is incredible low, there is no discount in the quality of services.

  • Windows 2012 R2
  • MSSQL 2012 and MySQL, ASP.NET 4.5.1/3.5/2.0, Visual Studio 2013
  • Sufficient Disk Space & Bandwidth, Isolated Application Pool
  • IIS Remote Management, MVC/Silverlight, PHP 7 and WordPress
  • USA or Europe Data Center
  • 24/7 Tech Support

HostForLIFE – a Superior ASP.NET Hosting Service Provider

HostForLIFE has been recognized as one of the best ASP.NET . For people who are unsatisfied with the services, HostForLIFE offers a 30 day money back guarantee allowing the clients to cancel their accounts within the first 30 days and getting a full refund. When it comes to the payment, two payment methods are available including CC and PayPal to ensure that every client reaches the services in a convenient way.

DiscountService – Premium ASP.NET Hosting Service Provider

DiscountService.biz supports Windows 2012/2008, ASP.NET 2.0/3.5SP1/4.0/4.5.1 as well as IIS8/ IIS7. It offers various versions of Microsoft SQL Databases, including MS SQL 2014, MS SQL 2012, MS SQL 2008R2 and MS SQL 2008. Each database comes with at least 500MB disk space. Furthermore, the webmasters can install the software by using one-click app installer. Besides, it is worth mentioning that the webmasters can get a full control of their websites through the users-friendly ASP.NET control panel of DiscountService.

ASP.NET MVC Hosting

ASP.NET MVC framework can be used for creating great web applications. By dividing an application into the model, the view, and the controller mvc make it easier to manage. MVC is a lightweight framework and integrated with all existing ASP.NET features, such as forms authentication and Windows authentication, URL authorization, membership and roles, output and data caching, session and profile state management, health monitoring, the configuration system, and the provider architecture. MVC design pattern aims to separate content from presentation and data-processing from content. This MVC separation helps you manage complex applications and simplifies group development. i.e. One developer can work on the view, a second developer can work on the controller logic, and a third developer can focus on the business logic in the model.

ASP.NET MVC Hosting Key Features:

  • Web hosting companies listed here have ASP.NET MVC installed and fully support the framework. Windows servers are ASP.NET MVC 6, MVC 4 and ASP.NET MVC 3 compatible.
  • Windows 2014 hosting with IIS 8 Hosting
  • MS SQL 2012 hosting, MS SQL 2014 hosting and the latest MS SQL 2016 hosting.
  • ASP.NET 1.1/ 2.0/3.5/ 4.0/5 hosting
  • URL Rewrite Module installed.

Save