【发布时间】:2019-01-16 00:55:13
【问题描述】:
我有以下控制器操作
[HttpPost]
[Route("api/Tenant/SetTenantActive")]
public async Task<IHttpActionResult> SetTenantActive(string tenantid)
{
var tenantStore = CosmosStoreFactory.CreateForEntity<Tenant>();
var allTenants = await tenantStore.Query().Where(x => x.TenantDomainUrl != null).ToListAsync();
foreach(Tenant ten in allTenants)
{
ten.Active = false;
await tenantStore.UpdateAsync(ten);
}
var tenant = await tenantStore.Query().FirstOrDefaultAsync(x => x.Id == tenantid);
if (tenant == null)
{
return NotFound();
}
tenant.Active = true;
var result = await tenantStore.UpdateAsync(tenant);
return Ok(result);
}
还有我的反应代码:
import React, { Component } from 'react';
import { Table, Radio} from 'antd';
import { adalApiFetch } from '../../adalConfig';
import Notification from '../../components/notification';
class ListTenants extends Component {
constructor(props) {
super(props);
this.state = {
data: []
};
}
fetchData = () => {
adalApiFetch(fetch, "/Tenant", {})
.then(response => response.json())
.then(responseJson => {
if (!this.isCancelled) {
const results= responseJson.map(row => ({
key: row.ClientId,
ClientId: row.ClientId,
ClientSecret: row.ClientSecret,
Id: row.Id,
SiteCollectionTestUrl: row.SiteCollectionTestUrl,
TenantDomainUrl: row.TenantDomainUrl
}))
this.setState({ data: results });
}
})
.catch(error => {
console.error(error);
});
};
componentDidMount(){
this.fetchData();
}
render() {
const columns = [
{
title: 'Client Id',
dataIndex: 'ClientId',
key: 'ClientId'
},
{
title: 'Site Collection TestUrl',
dataIndex: 'SiteCollectionTestUrl',
key: 'SiteCollectionTestUrl',
},
{
title: 'Tenant DomainUrl',
dataIndex: 'TenantDomainUrl',
key: 'TenantDomainUrl',
}
];
// rowSelection object indicates the need for row selection
const rowSelection = {
onChange: (selectedRowKeys, selectedRows) => {
if(selectedRows[0].key != undefined){
console.log(selectedRows[0].key);
const options = {
method: 'post',
body: {tenantid:selectedRows[0].key},
};
adalApiFetch(fetch, "/Tenant/SetTenantActive", options)
.then(response =>{
if(response.status === 200){
Notification(
'success',
'Tenant created',
''
);
}else{
throw "error";
}
})
.catch(error => {
Notification(
'error',
'Tenant not created',
error
);
console.error(error);
});
}
},
getCheckboxProps: record => ({
type: Radio
}),
};
return (
<Table rowSelection={rowSelection} columns={columns} dataSource={this.state.data} />
);
}
}
export default ListTenants;
只关注 onchange 事件,
看起来请求到达了 webapi(我附加了调试器)
更新:
基本上,如果我不输入 FromBody,我需要通过查询字符串发送参数。 但是,如果我从 Body 中输入并在 body 中发送参数,则它在 webapi 上收到 null
【问题讨论】:
-
乍一看,您的路线设置对我来说是正确的。我注意到如果
tenantStore.Query().FirstOrDefaultAsync(x => x.Id == tenantid)返回 null,您将返回 NotFound。验证此查询是否确实成功。很有可能此查询实际上失败了,因此您返回了 404。 -
但是当我附加调试器时它甚至没有达到断点
-
你是否可以直接使用 Postman、Insomnia、curl 或其他一些 REST 测试工具访问 api?试图找出是 API 问题还是您的客户端代码中的问题。
-
我想通了,基本上我需要创建一个发布请求,但是使用这样的查询字符串参数:/SetTenantActive?tenantid=1。但是,我的问题仍然存在,为什么它不接受正文中的数据?
-
@LuisValencia 尝试在输入参数之前添加
[FromBody],如下所示:public async Task<IHttpActionResult> SetTenantActive([FromBody]string tenantid)
标签: asp.net .net reactjs asp.net-web-api asp.net-web-api2