1.WebApiConfig配置API路由

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http; namespace WebAPIDemo1
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API 配置和服务 // Web API 路由
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
//api/controller控制器名/action方法名/id参数名
routeTemplate: "api/{controller}/{action}/{id}", // routeTemplate: "api/{controller}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}

2.配置Global.asax文件中Application_Start

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Routing; namespace WebAPIDemo1
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
}

3.创建PersonController控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPIDemo1.Models; namespace WebAPIDemo1.Controllers
{
public class PersonController : ApiController
{
#region GET
// /api/person/Get
public string[] Get()
{
return new string[] { "ru", "qq", "wx" };
}
// /api/person/Get?id=
public string Get(int id)
{
return "这是:" + id;
}
// /api/person/Get?name=
public string Get(string name)
{
return "姓名:" + name;
}
#endregion
#region Post
/// <summary>
/// 参数在地址栏上
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
// /api/person/Post?value=123
public string Post(string value)
{
return "Post请求值" + value;
}
/// <summary>
/// 参数不在地址栏上
/// </summary>
/// <param name="value"></param>
/// <returns>/api/person/Post </returns>
public string Post([FromBody]BodyValue value)
{
return "Post请求值" + value.value;
}
#endregion public string Put(int id, [FromBody] string value)
{
return "Put请求值:" + value + ",id:" + id;
} public string Delete(int id)
{ return "数据id为" + id + "删除成功";
}
//[HttpGet]
[HttpPost]
// /api/person/Login?Uid=admin&Pwd=123
public bool Login(string Uid, string Pwd)
{
if (Uid == "admin" && Pwd == "123")
return true;
else
return false;
} [HttpGet]
//入参实体参数FromUri
//api/person/Login2?Uid=admin&Pwd=456
public bool Login2([FromUri] UseInfo Model)
{
if (Model.Uid == "admin" && Model.Pwd == "123")
return true;
else
return false;
}
//Post 表单提交类型application/x-www-form-urlencoded
//body==>raw==>json==>{"Uid":"admin","Pwd":"123"}
//api/person/Login3
[HttpPost]
public bool Login3([FromBody] UseInfo Model)
{
if (Model.Uid == "admin" && Model.Pwd == "123")
return true;
else
return false;
} }
}

4.创建DogController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http; namespace WebAPIDemo1.Controllers
{
[RoutePrefix("api/Test")]
//第三种/api/Dog/Get
public class DogController : ApiController
{
[HttpGet]
//[Route("api/Dog/Get888")]第二种/api/Dog/Get888
//[Route("Get888")] //第一种/api/Test/Get888
//不加route /api/Dog/Get888
public string Get()
{
return "Get666";
} [HttpGet]
[Route("Login/{Uid}/{Pwd}")]
//加route /api/Test/Login/123/456
//参数地址化
public bool Login(string Uid,string Pwd)
{
if (Uid == "admin" && Pwd == "123")
return true;
else
return false;
}
}
}

WebAPI例子的更多相关文章

  1. 【ASP.NET MVC 牛刀小试】 URL Route

    例子引入 先看看如下例子,你能完全明白吗? using System; using System.Collections.Generic; using System.Linq; using Syste ...

  2. 【ASP.NET MVC系列】浅谈ASP.NET MVC 路由

    ASP.NET MVC系列文章 [01]浅谈Google Chrome浏览器(理论篇) [02]浅谈Google Chrome浏览器(操作篇)(上) [03]浅谈Google Chrome浏览器(操作 ...

  3. [.NET WebAPI系列01] WebAPI 简单例子

    [源] 来自微软WebAPI官方视频,Introduction to the ASP.NET Web API --Uniform Interface -- Demo-Using convention ...

  4. asp.net mvc项目创建WebApi简单例子

    1.创建默认路由的映射. namespace RedisDemo.App_Start { public class WebApiConfig { public static void Register ...

  5. ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-WebApi与Unity注入

    系列目录 前言: 有时候我们系统需要开放数据给手机App端或其他移动设备,不得不说Asp.net WebApi是目前首选 本节记录Asp.net MVC WebApi怎么利用Unity注入.系列开头已 ...

  6. webapi - 模型验证

    本次要和大家分享的是webapi的模型验证,讲解的内容可能不单单是做验证,但都是围绕模型来说明的:首先来吐槽下,今天下午老板为自己买了套新办公家具,看起来挺好说明老板有钱,不好的是我们干技术的又成了搬 ...

  7. WebApi接口 - 如何在应用中调用webapi接口

    很高兴能再次和大家分享webapi接口的相关文章,本篇将要讲解的是如何在应用中调用webapi接口:对于大部分做内部管理系统及类似系统的朋友来说很少会去调用别人的接口,因此可能在这方面存在一些困惑,希 ...

  8. WebApi接口 - 响应输出xml和json

    格式化数据这东西,主要看需要的运用场景,今天和大家分享的是webapi格式化数据,这里面的例子主要是输出json和xml的格式数据,测试用例很接近实际常用情况:希望大家喜欢,也希望各位多多扫码支持和点 ...

  9. C#进阶系列——WebApi 接口返回值不困惑:返回值类型详解

    前言:已经有一个月没写点什么了,感觉心里空落落的.今天再来篇干货,想要学习Webapi的园友们速速动起来,跟着博主一起来学习吧.之前分享过一篇 C#进阶系列——WebApi接口传参不再困惑:传参详解  ...

  10. Restful WebApi项目开发实践

    前言 踩过了一段时间的坑,现总结一下,与大家分享,愿与大家一起讨论. Restful WebApi特点 WebApi相较于Asp.Net MVC/WebForm开发的特点就是前后端完全分离,后端使用W ...

随机推荐

  1. 结合商业项目深入理解Go知识点

    这篇文章比较硬核,爆肝5千字,把之前整理的知识点都串起来了.建议先收藏,慢慢看. 前言 上一篇文章 #[Go WEB进阶实战]开源的电商前后台API系统 很受大家欢迎,有好多小伙伴私信我问题:&quo ...

  2. [机器学习] PCA主成分分析原理分析和Matlab实现方法

    转载于http://blog.csdn.net/guyuealian/article/details/68487833 网上关于PCA(主成分分析)原理和分析的博客很多,本博客并不打算长篇大论推论PC ...

  3. Pytorch基础-张量基本操作

    一,张量的基本操作 二,维度变换 2.1,squeeze vs unsqueeze 维度增减 2.2,transpose vs permute 维度交换 三,索引切片 3.1,规则索引切片方式 3.2 ...

  4. [WPF]程序随系统自启动

    代码 Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\ ...

  5. three.js实现分模块添加梦幻bloom辉光光晕方案--详细注释版本~~方案三版本~~

    先上图对比方案1-2-3不同点,本文是方案3 方案1(旋转场景情况下发光体不应该遮住另一个,但是遮住了) 方案2(层次正常,发光正常) 方案3(层次正常,发光正常,但是转动场景时候部分辉光会被遮挡,但 ...

  6. Angular在用户登录后设置授权请求头headers.append('Authorization', 'token');

    方案1. 使用Angular  http import {Injectable} from '@angular/core'; import {Http, Headers} from '@angular ...

  7. linux基础(部分讲解)

    linux常见岗位 Linux运维工程师.Linux高级架构师.运维开发工程师.数据库管理员.云计算架构师 作为python后端可以从事的linux岗位: 自动化运维.容器运维.DBA 注意:会的越多 ...

  8. Flink1.15仅支持ZooKeeper3.5/3.6

    这是一个验证贴,因为社区文档是错误的. 先说结论 Flink1.15仅支持ZooKeeper3.5/3.6,不再支持3.4.FLINK-25146 Drop support for Zookeeper ...

  9. (Newtonsoft)Json增删改查

    public static class JsonHelper { #region 字段 private static string json; public static string path; # ...

  10. 基于APIView&ModelSerializer写接口

    目录 基于APIView&ModelSerializer写接口 一.首先准备前提工作 1.模型代码 2.路由代码 3.视图代码 二.继承Serializer序列化定制字段的三种方法 1.通过s ...