owin通过ApiController获取请求参数
时间: 2023-06-15 21:07:44 浏览: 76
在 ASP.NET Web API 中,可以通过 `ApiController` 类来获取请求参数,具体步骤如下:
1. 定义一个继承自 `ApiController` 的控制器类。
```csharp
public class MyController : ApiController
{
// ...
}
```
2. 在控制器类中定义一个或多个公共方法,并使用 `HttpGet`、`HttpPost`、`HttpPut`、`HttpDelete` 等属性来标识该方法对应的 HTTP 请求方法。
```csharp
public class MyController : ApiController
{
[HttpPost]
public IHttpActionResult MyAction(string arg1, int arg2)
{
// ...
}
}
```
3. 在方法中使用 `[FromBody]`、`[FromUri]` 等属性来标识参数的来源,并将参数定义为方法的参数。
```csharp
public class MyController : ApiController
{
[HttpPost]
public IHttpActionResult MyAction([FromBody] MyModel model)
{
// ...
}
}
```
以上就是通过 `ApiController` 获取请求参数的基本步骤。需要注意的是,如果参数来源是请求正文,则必须使用 `[FromBody]` 属性来标识,否则 Web API 将默认从 URL 查询字符串中获取参数。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)