ASP.NET MVC - Web API

ASP.NET MVC - Web API 首页 / ASP.Net MVC入门教程 / ASP.NET MVC - Web API

ASP.NET Web API是一个框架,可轻松构建可访问包括浏览器和移动设备在内的广泛客户端的HTTP服务,ASP.NET Web API是在.NET Framework上构建RESTful应用程序的理想平台。

通过创建一个新的ASP.NET Web应用程序,让我们看一个简单的Web API示例。

步骤1 - 打开Visual Studio,然后单击File→New→Item菜单选项。

将打开一个"New Project"对话框。

Visual Studio Click File

步骤2 - 在左侧窗格中,选择Template→Visual C#→Web。

步骤3 - 在中间窗格中,选择ASP.NET Web应用程序

在Name字段中输入项目名称WebAPIDemo,然后单击确定以继续。您将看到以下对话框,要求您设置ASP.NET项目的初始内容。

WebAPIDemo

步骤4 - 为了简化操作,请选择"Empty"选项,然后选中"Add folders and core references for"部分中的" Web API"复选框,然后单击"OK"。

步骤5 - 它将创建具有最少预定义内容的基本MVC项目。

通过Visual Studio创建项目后,您将在"Solution Explorer"窗口中看到许多文件和文件夹。

步骤6 - 现在我们需要添加一个模型。在Solution Explorer中的Models文件夹上单击鼠标右键,然后选择Add→Class。

Add a Model

现在,您将看到"Add New Item"对话框。

无涯教程网

Add Item Dialog

步骤7 - 在中间窗格中选择"Class",然后在名称字段中输入Employee.cs。

步骤8 - 使用以下代码向Employee类添加一些属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebAPIDemo.Models {
   public class Employee{
      public int ID { get; set; }
      public string Name { get; set; }
      public DateTime JoiningDate { get; set; }
      public int Age { get; set; }
   }
}

步骤9 - Add Controller。右键单击Solution Explorer中的controller文件夹,然后选择Add→Controller。

它将显示"Add Scaffold"对话框。

Select Add Controller

步骤10 - 选择" Web API 2 Controller- Empty"选项,该模板将使用控制器的默认操作创建一个Index方法。

链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_mvc/asp.net-mvc-web-api.html

来源:LearnFk无涯教程网

步骤11 - 单击"Add"按钮,将出现"Add Controller"对话框。

 Add Button Controller

步骤12 - 将名称设置为EmployeesController,然后单击"Add"按钮。

您将在Controllers文件夹中看到一个新的C#文件" EmployeeController.cs",该文件可以在Visual Studio中使用一些默认操作进行编辑。

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web.Http;
using WebAPIDemo.Models;

namespace WebAPIDemo.Controllers{
   public class EmployeesController : ApiController{
      Employee[] employees = new Employee[]{
         new Employee { ID = 1, Name = "Mark", JoiningDate =
            DateTime.Parse(DateTime.Today.ToString()), Age = 30 },
         new Employee { ID = 2, Name = "Allan", JoiningDate =
            DateTime.Parse(DateTime.Today.ToString()), Age = 35 },
         new Employee { ID = 3, Name = "Johny", JoiningDate =
            DateTime.Parse(DateTime.Today.ToString()), Age = 21 }
      };
		
      public IEnumerable<Employee> GetAllEmployees(){
         return employees;
      }
		
      public IHttpActionResult GetEmployee(int id){
         var employee = employees.FirstOrDefault((p) => p.ID == id);
         if (employee == null){
            return NotFound();
         }
         return Ok(employee);
      }
   }
}

步骤13 - 运行此应用程序,并在URL末尾指定/api/employees /,然后按回车,您将看到以下输出。

Specify API Employees

步骤14 - 让我们指定以下URL http://localhost:63457/api/employees/1 ,您将看到以下输出。

Localhost API Employees

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

邱岳的产品手记 -〔邱岳〕

Nginx核心知识150讲 -〔陶辉〕

Linux性能优化实战 -〔倪朋飞〕

Linux内核技术实战课 -〔邵亚方〕

基于人因的用户体验设计课 -〔刘石〕

说透数字化转型 -〔付晓岩〕

Redis源码剖析与实战 -〔蒋德钧〕

PyTorch深度学习实战 -〔方远〕

结构会议力 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)