Include WebAPI to an existent project with ASP.NET MVC

If you have a new project and wan’t to create a project with MVC and WebAPI together, you just need to select the correct template on Visual Studio.
But and if you already have an MVC project and wan’t to include an WebAPI on the same project. How can you do that?

1. Add reference to 
System.Web.Http.WebHost.
2. Add App_Start\WebApiConfig.cs (see code snippet below).
3. Import namespace System.Web.Http in Global.asax.cs.
4. Call WebApiConfig.Register(GlobalConfiguration.Configuration) in MvcApplication.Application_Start() (in file Global.asax.cs), before registering the default Web Application route as that would otherwise take precedence.
5. Add a controller deriving from System.Web.Http.ApiController.


I could then learn enough from 
the tutorial  (Your First ASP.NET Web API) to define my API controller.

App_Start\WebApiConfig.cs:

using System.Web.Http;
class WebApiConfig
{
    public static void Register(HttpConfiguration configuration)
    {
        configuration.Routes.MapHttpRoute("API Default", "api/{controller}/{id}",
            new { id = RouteParameter.Optional });
    }
}

Global.asax.cs:

using System.Web.Http;
...
protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}


The NuGet package Microsoft.AspNet.WebApi must be installed for the above to work.

Advertisements

ASP.NET 5 – Create Web Application Step by Step

Introduction

Using MVC, Entity Framework, ASP.NET Scaffolding you can create a web application that stores your information. This demo shows you how to create a ASP.NET 5 web application with MVC and Entity Framework 7.

STEP 1 – Create ASP.NET 5 Web Application

  • Open Visual Studio 2015 and create a new project of type ASP.NET 5 Web Application.
  • On this project I create a solution called Demo.

  • Press OK, and a new screen will appear, with several options of template to use on our project.
  • Select the option MVC.

After selection of our template, your first web application using ASP.NET 5 is created.

 

STEP 2 – Create Data Model

After we have our web application created, we need to ceate our data model.

For that, select the option Add New Item on solution and choose the option Class. Create the class like the one on the image above.

 

STEP 3 – Scaffolding

This could be made easily using the Scaffolding functionality.

On the solution on the top of controller folder, select the option Add New Scaffold Item.

 

On the new screen, select the option MVC6 Controller with views using entity framework.

 

Select the name of the controller, class model and data context class.

The new controllers and views associated, was created with sucess.

 

STEP 4 – Change Menu Layout

To test the two tables, we can add two new entries on the web application menu.

 

STEP 5 – Run Application

Press now the F5 button, to run the web application.

The new entities appear on the menu.

Press the option car to see our entiity in action

An message error will appear sayng that the entity doesn’t exists on internal database and that we need to create it using some commands on package manager console.

Execute the commands like on the image above na try again

An thats it! Now its working

Resources:
Code – https://code.msdn.microsoft.com/ASPNET-5-Create-Web-b8a44cce

ASP.NET MVC 5 – Bootstrap 3.0 in 3 Steps

This article walks you through the steps for creating a ASP.NET MVC  5 Web Application using Bootstrap as template for layout.

Read more of this post

ASP.NET MVC 5 – Connect with Azure SQLServer Database

Using MVC, Entity Framework, ASP.NET Scaffolding, and Azure SQLServer you can create a web application that stores your information on an SQL Azure database. This demo shows you how to create a web application with MVC and Entity Framework 6, that communicate with a SQL Azure database.

Read more of this post

ASP.NET WebAPI 2 – Stream Windows Azure blobs

With this article, I will show all steps you need to complete to create an ASP.NET WebAPI 2 to upload and download your documents to an Windows Azure Storage.

Read more of this post

How to Publish ASP.NET MVC Web Application to Windows Azure

With this article, I will show every steps you need to complete to deploy an ASP.NET MVC application to a Windows Azure Web Site by using the Publish Web Wizard that exists on Visual Studio 2013. Read more of this post

ASP.NET MVC 5 – Routing

Routing is how ASP.NET MVC matches a URI to an Action.

ASP.NET MVC 5 has a new feature called “Attribute Routing” that allows a developer to specify the route of controller actions by adding Route Attributes to them. Developers can also add a RoutePrefix Attribute on the controller if each of the controller actions within that controller share a particular route prefix. I’ll show a very basic example of this using a SampleController that will specify both a RoutePrefix on the controller as well as Route Attributes on each controller action. Read more of this post