webAPI路由的使用
根据WEBAPI的路由规则,在实际项目当中有二种用法,
一:webAPI.Config里面为
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
控制器里面加上【HttpGet,HttpPOST】认证,并将接口使用路由暴露出来,写法有两种
(1)在控制器上面加上路由前缀,action之前加上action名字从而将整个路由暴露出来,方便前端调用
[RoutePrefix("api/Values")]
public class ValuesController : ApiController
{
// GET api/values
[HttpGet]
[Route("testLog")]
public IEnumerable<string> testLog()
{
LogHelper.WriteLog("hello can you hear me~");
return new string[] { "value1", "value2" };
}
(2)直接在action之前加上整个路由路径从而将整个路由暴露出来
public class ValuesController : ApiController
{
// GET api/values
[HttpGet]
[Route("api/Values/testLog")]
public IEnumerable<string> testLog()
{
LogHelper.WriteLog("hello can you hear me~");
return new string[] { "value1", "value2" };
}
二:webAPI.Config里面为
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
控制器里面加上【HttpGet,HttpPOST】认证, 不需要使用路由暴露接口
[HttpGet]
public IEnumerable<string> testLog()
{
LogHelper.WriteLog("hello can you hear me~");
return new string[] { "value1", "value2" };
}
两种情况,如果不使用【httpPost】【httpGet】,后台会根据前台的请求类型匹配控制器的action,如get请求,不管前台访问哪个接口,后台均匹配以Get开头的控制器action
webAPI路由的使用的更多相关文章
- C#进阶系列——WebApi 路由机制剖析:你准备好了吗?
前言:从MVC到WebApi,路由机制一直是伴随着这些技术的一个重要组成部分. 它可以很简单:如果你仅仅只需要会用一些简单的路由,如/Home/Index,那么你只需要配置一个默认路由就能简单搞定: ...
- mvc webapi路由重写
修改app_start/webapiconfig.cs using System.Web.Http; using System.Web.Routing; using Ninject; using Tx ...
- 【C#】 WebApi 路由机制剖析
C#进阶系列——WebApi 路由机制剖析:你准备好了吗? 转自:https://blog.csdn.net/wulex/article/details/71601478 2017年05月11日 10 ...
- C#进阶系列——WebApi 路由机制剖析:你准备好了吗? 转载https://www.cnblogs.com/landeanfen/p/5501490.html
阅读目录 一.MVC和WebApi路由机制比较 1.MVC里面的路由 2.WebApi里面的路由 二.WebApi路由基础 1.默认路由 2.自定义路由 3.路由原理 三.WebApi路由过程 1.根 ...
- WebApi 路由机制剖析
阅读目录 一.MVC和WebApi路由机制比较 1.MVC里面的路由 2.WebApi里面的路由 二.WebApi路由基础 1.默认路由 2.自定义路由 3.路由原理 三.WebApi路由过程 1.根 ...
- WebApi路由机制详解
随着前后端分离的大热,WebApi在项目中的作用也是越来越重要,由于公司的原因我之前一直没有机会参与前后端分离的项目,但WebApi还是要学的呀,因为这东西确实很有用,可单独部署.与前端和App交互都 ...
- MVC和WebApi路由机制比较
1.MVC使用的路由 在MVC中,默认路由机制是通过解析url路径来匹配Action.比如:/User/GetList,这个url就表示匹配User控制器下的GetList方法,这是MVC路由的默认解 ...
- WebApi - 路由
这段时间的博客打算和大家一起分享下webapi的使用和心得,主要原因是群里面有朋友说希望能有这方面的文章分享,随便自己也再回顾下:后面将会和大家分不同篇章来分享交流心得,希望各位多多扫码支持和点赞,谢 ...
- WebApi:路由和Action选择
译自:http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection ...
- Asp.Net Webapi路由基本设置
1.直接在Global.asax中添加配置 如: using MvcApplication4.App_Start; using System; using System.Collections.Gen ...
随机推荐
- 【leetcode刷题笔记】Spiral Matrix II
Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...
- hadoop 学习笔记:mapreduce框架详解(转)
原文:http://www.cnblogs.com/sharpxiajun/p/3151395.html(有删减) Mapreduce运行机制 下面我贴出几张图,这些图都是我在百度图片里找到的比较好的 ...
- 剑指offer——翻转单词顺序VS左旋转字符串
字符串的交换等,注意判断字符串的是否为NULL,以及判断边界等. #include <iostream> #include <string> using namespace s ...
- 《机器学习实战》学习笔记第十四章 —— 利用SVD简化数据
相关博客: 吴恩达机器学习笔记(八) —— 降维与主成分分析法(PCA) <机器学习实战>学习笔记第十三章 —— 利用PCA来简化数据 奇异值分解(SVD)原理与在降维中的应用 机器学习( ...
- codevs1199 开车旅行
[问题描述]小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为H i ,城市 ...
- form 中Enctype=multipart/form-data 的作用
form 中Enctype=multipart/form-data 的作用 ENCTYPE="multipart/form-data"用于表单里有图片上传. <form na ...
- poj3177边-双连通分量
题意和poj3352一样..唯一区别就是有重边,预先判断一下就好了 #include<map> #include<set> #include<list> #incl ...
- linux应用之yum命令的软件源的更换(centos)
[1] 首先备份/etc/yum.repos.d/CentOS-Base.repo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/Cent ...
- MySQL--Basic(二)
USE db_name; CREATE DATABASE school; Use school; CREATE TABLE `StuInfo` ( `STU_ID` ) NOT NULL , `STU ...
- [原]NYOJ-216-A problem is easy
大学生程序代写 /*A problem is easy 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 When Teddy was a child , he was a ...