Create and Publish ASP.NET Web API in Azure (2024)

Introduction

This article will give you step-by-step instructions for creating an ASP.NET Web API service, using Entity Framework, and publishing it in Azure.

Prerequisites

Basic knowledge of ASP.NET Web API, Entity Framework, Kendo UI Framework, and Azure Web apps.

Create an SQL Database in Azure

Let's start with creating a database in Azure. Please check here howto create a new database in Azure.

I have created a new database in Azure. Connect the database in SSMS (SQL Studio Management Service), as shown in the image below.

Create and Publish ASP.NET Web API in Azure (1)

The script for creating a table is given below.

CREATE TABLE Employee( EmployeeID INT IDENTITY(1,1) CONSTRAINT Pk_Emp_Id PRIMARY KEY, FirstName VARCHAR(20), LastName VARCHAR(20));

Insert some sample records, as shown below.

INSERT INTO Employee VALUES('Bob','Ross');INSERT INTO Employee VALUES('Pradeep','Raj');INSERT INTO Employee VALUES('Arun','Kumar');INSERT INTO Employee VALUES('Vasanth','Kumar');

Create a new ASP.NET Web API application

Create a new ASP.NET Web API Application, as per the following figures. Open Visual Studio ->File ->New project ->ASP.NET Web Application.

Create and Publish ASP.NET Web API in Azure (2)

Create and Publish ASP.NET Web API in Azure (3)

Make sure, the host in Cloud is checked and click OK.

Create and Publish ASP.NET Web API in Azure (4)

Once Azure Web app settings are configured, click OK.

Let us start creating a Model in the application.

Generate the Model

Now, we will create Entity Framework Models from the database tables.

Step 1.Right-click the Models folder, select Add, and New Item.

Step 2.In the Add New Items window, select data in the left pane and ADO.NET Entity Data Model from the center pane. Name the new Model file as Employee and click Add.

Step 3.In the Entity Data Model wizard, select EF Designer from the database and click Next.

Create and Publish ASP.NET Web API in Azure (5)

Step 4.Click the New Connection button.

Step 5.In the Connection Properties window, provide the name of the Azure SQL Server where the database was created. After providing the Server name, select Employee from the available databases and click OK.

Create and Publish ASP.NET Web API in Azure (6)

Step 6.You can use the default name for connection to save on the Web. Config the file and click Next.

Step 7.Select the table to generate models for the Employee table and click Finish.

Create a Controller

Create a new empty Controller. Right-click the Controllers folder and select Add –> New Empty Web API 2 Controller. In my case, I named it as EmployeeCRUD Controller.

Write the code, given below, in the Controller file.

public class EmployeesController : ApiController{ private EmployeeEntities db = new EmployeeEntities(); // GET: api/Employees public IQueryable<Employee> GetEmployees() { return db.Employees; } // PUT: api/Employees/5 public HttpResponseMessage PutEmployee(Employee employee) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } db.Entry(employee).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); } // POST: api/Employees public HttpResponseMessage PostEmployee(Employee employee) { if (!ModelState.IsValid) { db.Employees.Add(employee); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, employee); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = employee.EmployeeID })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } } // DELETE: api/Employees/5 public HttpResponseMessage DeleteEmployee(Employee employee) { Employee remove_employee = db.Employees.Find(employee.EmployeeID); if (remove_employee == null) { return Request.CreateResponse(HttpStatusCode.NotFound); } db.Employees.Remove(remove_employee); try { db.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } return Request.CreateResponse(HttpStatusCode.OK); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } private bool EmployeeExists(int id) { return db.Employees.Count(e => e.EmployeeID == id) > 0; }}

Create a new HTML page in the project where we implemented Kendo Grid with the CRUD operation, to test the REST API Service after publishing the Application.

EmployeeGrid.html

<!DOCTYPE html><html><head> <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.common.min.css" /> <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.default.min.css" /> <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.min.css" /> <link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1316/styles/kendo.dataviz.default.min.css" /> <script src="http://cdn.kendostatic.com/2014.3.1316/js/jquery.min.js"></script> <script src="http://cdn.kendostatic.com/2014.3.1316/js/kendo.all.min.js"></script> <title></title></head><body> <script> $(document).ready(function () { dataSource = new kendo.data.DataSource({ transport: { read: { url: "/api/Employees", dataType: "json", }, destroy: { url: "/api/Employees", type: "DELETE" }, create: { url: "api/Employees", type: "POST" }, update: { url: "api/Employees", type: "PUT", parameterMap: function (options, operation) { if (operation !== "read" && options.models) { return { models: kendo.stringify(options.models) }; } } }, }, schema: { model: { id: "EmployeeID", fields: { EmployeeID: { editable: false, nullable: true, type: "number" }, FirstName: { editable: true, nullable: true, type: "string" }, LastName: { editable: true, nullable: true, type: "string" }, } } } }); $("#grid1").kendoGrid({ dataSource: dataSource, editable: "inline", toolbar: ["create"], columns: [ { field: "EmployeeID", title: "Employee ID" }, { field: "FirstName", title: "First Name" }, { field: "LastName", title: "Last Name" }, { command: ["edit", { name: "destroy", text: "remove", } ], } ], height: "500px", pageable: { refresh: true, pageSizes: true, buttonCount: 5 }, }).data("kendoGrid"); }); </script> <div class="main-content"> <div id="grid1"></div> </div></body></html>

Now, it’s time to publish the app.

Create and Publish ASP.NET Web API in Azure (7)

Right-click on the project in Solution Explorer and click Publish.

Create and Publish ASP.NET Web API in Azure (8)

Select the recently created Web app for the Application and click OK.

Check the ApplicationDbContext and EmployeeEntities and click Next.

Create and Publish ASP.NET Web API in Azure (9)

Click Publish to start the process.

Once the publishing is completed, just test the REST API Service, using Fiddler/PostMan.

Testing the GET service

URL:myapi5942.azurewebsites.net/api/Employees

Response

Create and Publish ASP.NET Web API in Azure (10)

Test the CRUD operation in Kendo Grid, using the Services, which are published in Azure.

GET

URL: myapi5942.azurewebsites.net/api/Employees

Type:GET

Create and Publish ASP.NET Web API in Azure (11)

CREATE

URL:myapi5942.azurewebsites.net/api/Employees

Type: POST

Create and Publish ASP.NET Web API in Azure (12)

DELETE

URL:myapi5942.azurewebsites.net/api/Employees

Type: Delete

Create and Publish ASP.NET Web API in Azure (13)

UPDATE

URL: myapi5942.azurewebsites.net/api/Employees

Type: PUT

Create and Publish ASP.NET Web API in Azure (14)

Conclusion

We have seen how to create ASP.NET Web API in Azure App Service and we went through the quick demo of CRUD operation in Kendo Grid which consumes the REST API services that we have created and deployed as Azure Web app.

I hope you enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.

Create and Publish ASP.NET Web API in Azure (2024)

FAQs

How to publish ASP.NET Web API to Azure? ›

Publish the web API to Azure App Service
  1. In Solution Explorer, right-click the project and select Publish.
  2. In the Publish dialog, select Azure and select the Next button.
  3. Select Azure App Service (Windows) and select the Next button.
  4. Select Create a new Azure App Service. ...
  5. Select the Create button.
Nov 3, 2022

How to create a Web API in Azure? ›

Create an API
  1. Navigate to your API Management service in the Azure portal and select APIs from the menu.
  2. From the left menu, select + Add API.
  3. Select HTTP from the list.
  4. Select Create.
Jun 30, 2022

How to create asp net Web API? ›

  1. From the File menu, select New > Project.
  2. Enter Web API in the search box.
  3. Select the ASP.NET Core Web API template and select Next.
  4. In the Configure your new project dialog, name the project TodoApi and select Next.
  5. In the Additional information dialog: Confirm the Framework is .NET 8.0 (Long Term Support).
Jul 16, 2024

How to deploy asp net web application in Azure VM? ›

Deploy and run the app
  1. In Solution Explorer, right-click your project, and then select Publish.
  2. In the Publish window, select New.
  3. Select Web Server (IIS).
  4. Select Next > Web Deploy > Next.
  5. For Server enter the DNS name that you defined earlier, such as mywebapp.region.cloudapp.azurestack.corp.contoso.com .

Where to deploy asp net web API? ›

You can just deploy the runtime assemblies in the \bin folder of your ASP.NET Web API application. For example, in order to deploy the SofiaCarRentalWebApp application to a web server (e.g. IIS), you need to ensure that the following Telerik Data Access assemblies are copied in the \bin folder: Telerik. OpenAccess.

How do I deploy Web API in Azure API Management? ›

In this module, you will:
  1. Create an Azure API gateway.
  2. Import an API to the API gateway.
  3. Publish an API ready for developer access.
  4. Call an API with a subscription key.

What is the difference between ASP NET Web API and ASP NET core Web API? ›

ASP.NET API mainly uses IIS (Internet Information Services) for web hosting, which is great for Windows environments. . NET CORE API offers more flexibility with a variety of hosting options including self-hosted, docker containers, and cloud platforms.

How do I import an API into Azure? ›

When you import another API, the operations are appended to your current API.
  1. Go to your Azure API Management instance in the Azure portal.
  2. Select APIs on the Overview page or from the menu on the left.
  3. Click ... ...
  4. Select Import from the drop-down menu.
  5. Select a service from which to import an API.
May 23, 2024

How to deploy a web app in Azure App Service? ›

Example: deploy using Web Deploy
  1. Select the Tasks tab, then select Deploy Azure App Service. ...
  2. In the dialog, make sure that Connection type is set to Azure Resource Manager.
  3. In the dialog, expand Additional Deployment Options and select Select deployment method. ...
  4. Save the release pipeline.
Jun 4, 2024

What is the difference between Web API and REST API? ›

REST APIs use the HTTP protocol to send and receive data. Web APIs, on the other hand, rely on multiple communication protocols like SOAP, XML-RPC, and JSON-RPC. REST APIs are the most widely used for data integration, facilitating efficient data transfer.

Is ASP.NET Web API RESTful? ›

While the Web API can be used to create a RESTful interface, it is not inherently RESTful.

How to create ASP.NET Core Web API in command line? ›

How to Create an ASP.NET Core Web API
  1. Step 1: Setting up the Environment. First, you must have the .NET Core SDK installed on your computer. ...
  2. Step 2: Creating a New Project. Now, let's create a new Web API project. ...
  3. Step 3: Understanding the Project Structure. ...
  4. Step 4: Adding a New Controller. ...
  5. Step 5: Running the API.
Jul 25, 2023

How to host asp net API on Azure? ›

Deploy the app self-contained
  1. Right-click the project in Solution Explorer and select Publish. ...
  2. In the Publish dialog, select Azure > Next.
  3. Select the Azure service.
  4. Select Advanced. ...
  5. Select a Resource group and Hosting plan, or create new ones.
  6. Select Finish.
  7. In the Publish page:
Jun 21, 2024

How to deploy ASP.NET web application in cloud? ›

Creating an ASP.NET app
  1. Choose File > New > Project to open the New Project dialog.
  2. Use the left panel to navigate to Installed > Visual C# > Google Cloud Platform.
  3. Select ASP.NET on Google Cloud Platform.
  4. Enter the Name of your project.
  5. Click OK.
  6. Select the Template type.
  7. Click OK.

How to publish a .NET web application? ›

Select the Publish button.
  1. In a command shell, publish the app in Release configuration with the dotnet publish command: dotnet publish --configuration Release.
  2. Move the contents of the bin/Release/{TARGET FRAMEWORK}/publish folder to the IIS site folder on the server, which is the site's Physical path in IIS Manager.
Sep 17, 2023

How do I upload a Webapp to Azure? ›

STEPS:
  1. Step 1: Create an App Service Plan in Azure. ...
  2. Step 2: Create a new web application project in Visual Studio Code. ...
  3. Step 3: Deploy the application to Azure App Service. ...
  4. Step 4: Test the application. ...
  5. 17 Mindblowing Python Automation Scripts I Use Everyday.
Jan 12, 2023

How do I publish an API to the cloud? ›

Select the API product that you want to add to your portal. Click Next. The API details page displays. Select Published to publish the API to your portal.

How to publish swagger API to Azure? ›

Open the Azure portal, https://portal.azure.com.
  1. On the left, select All resources.
  2. Click your service instance in the list.
  3. On the left, select Management API.
  4. Select the Enable API Management REST API check box.
  5. At the bottom, under Access token, click Generate.

References

Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6496

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.