- 旅游网站解决方案
- SQL SERVER 企业管理器 MMC 无法创建管理单元
- FCKeditor在ASP.NET中是用伪静态时的配置方法
- URL中的hash(井号#)
- 企业网站解决方案
- thinkPHP中模型类的定义
- 谁知道电商运营工资大概是怎么算的比较合理?
- 选择关键词应该注意Stop Words
- 简单实用的CSS网页布局中文排版心得
- 经典SQL语句大全之应用篇
邮箱:
手机:15383239821
webapi中请求地址
在我们创建的Controllers里面的xxxContoller会看到 [Route("api/[controller]")]这样的标识,
这段代码实际Route 规定了我们的路由,路由体现在URL里
http://localhost/api/controller?query
[controlller]中括号 差不多就是代表占位符
示例代码1:
[ApiController]
[Route("api/[controller]/[action]")]
public class TestController : ControllerBase
{
[HttpGet]
public string GetData()
{
return "1";
}
}
上面代码中,Url不区分大小写 [action] 是你的方法名字 GetData
那么我们访问的url就是 http://localhost/api/test/getdata
去掉action后,变成 [[Route("api/[controller]")]] ,那么我们的url就是
http://localhost/api/test
去掉api/的时候,成为 [[Route("[controller]")]] ,那么我们的url就是
http://localhost/test
示例代码2:
[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
//出去使用[action]占位的方式还有两种方式 第一种直接[HttpGet("xxxx")]
/*
[HttpGet("xxx")]
public string GetData()
{
return "1";
}*/
//第二种使用[Route("xxx")]
[HttpGet]
[Route("xxxx")]
public string GetData()
{
return "1";
}
}
我们代码里会有不止一个Get请求,那么我们需要区分每个方法,看代码:
[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
// 除去使用[action]占位的方式还有两种方式 第一种直接[HttpGet("xxxx")]
/*
[HttpGet("xxx")]
public string GetData()
{
return "1";
}*/
//第二种使用[Route("xxx")]
[HttpGet]
[Route("xxxx")]
public string GetData()
{
return "1";
}
}
那么我们访问的url就是
http://localhost/api/test/xxx
还有个Name 属性
Name,目前测试可以用于寻址,其它作用不明 ,有兴趣去查查官方文档吧
[HttpGet(Name = "GetWeatherForecast")]
第二个参数啥意思我也不知道,没研究过也没用过这个
string uri = Url.Link("GetWeatherForecast","");
//输出
https://localhost:7049/WeatherForecast?Length=0
接收参数
接收参数的方法大概以下几种
FromBody //application/json
FromForm //前端的默认消息类型
FromHeader //从请求头里获取
FromQuery //从参数里获取
FromRoute //从路由中获取
FromServices //这个Jwt涉及到
-
2010-10-23cook 的设置
-
2013-04-15什么是MX记录
-
2019-11-19button对象如何使用disabled属性
-
2012-07-02如何优化指数在1000以上的关键词
-
2024-04-19将 WhatsApp 聊天添加到您的网站
-
2023-09-19PHP str_replace() 函数
