控制器从application / json负载敏感地接受数据大小写

时间:2019-06-01 09:52:56

标签: c# asp.net-mvc asp.net-core

我正在用OData编写WebApi,但遇到了问题。问题是控制器仅区分大小写接受数据。例如,当我通过{"name":"London"}时,它给出一个空对象和一个错误The property 'name' does not exist on type 'Taxes.Service.DataLayer.Models.Municipality'. Make sure to only use property names that are defined by the type.。但是,当我通过{"Name":"London"}时,一切正常-创建对象并设置值。实际上不知道可能出什么问题。谢谢。

BaseController.cs

public class BaseController<T> : ODataController where T : BaseModel
{
    protected readonly TaxesContext Context;

    public BaseController(TaxesContext context)
    {
        Context = context;
    }

    [HttpPost]
    public async Task<IActionResult> Post([FromBody]T baseObject)
    {
        if (!ModelState.IsValid)
        {
            var errors = ModelState
                .SelectMany(x => x.Value.Errors, (y, z) => z.Exception.Message);

            return BadRequest(errors);
        }

        await Context.Set<T>().AddAsync(baseObject);
        await Context.SaveChangesAsync();
        return Created(baseObject);
    }
}

BaseModel.cs

public class BaseModel
{
    public int Id { get; set; }
}

Municipality.cs

public class Municipality : BaseModel
{
    public string Name { get; set; }
    public ICollection<Tax> Taxes { get; set; }
}

邮递员请求 enter image description here

1 个答案:

答案 0 :(得分:1)

使用以下语句添加camelCase支持:import React from 'react' import TreeMenu from 'react-simple-tree-menu' import Form from 'react-bootstrap/Form' import ListGroup from 'react-bootstrap/ListGroup' import ListGroupItem from 'react-bootstrap/ListGroupItem' const DEFAULT_PADDING = 16 const ICON_SIZE = 8 const LEVEL_SPACE = 16 const ToggleIcon = ({ on }) => <span style={{ marginRight: 8 }}>{on ? '-' : '+'}</span> const ListItem = ({ level = 0, hasNodes, isOpen, label, searchTerm, openNodes, ...props }) => ( <ListGroupItem {...props} style={{ paddingLeft: DEFAULT_PADDING + ICON_SIZE + level * LEVEL_SPACE, cursor: 'pointer', }}> {hasNodes && <ToggleIcon on={isOpen}/>} <img src={props.url} width={32} height={32} alt={label}/> &nbsp; {label} </ListGroupItem> ) class MyTreeMenu extends React.Component { constructor (props){ super(props) this.processSelection = this.props.processSelection.bind(this) } render () { return ( <TreeMenu data={this.props.data} debounceTime={125} onClickItem={({ key, label, ...props }) => {this.processSelection(props.modifier, props.slug)}}> {({ search, items }) => ( <> <Form> <Form.Group controlId="formSearch"> <Form.Control onChange={e => search(e.target.value)} placeholder="Type and search"/> </Form.Group> </Form> <ListGroup> { items.map(props => (<ListItem {...props} />)) } </ListGroup> </> )} </TreeMenu> ) } } export default MyTreeMenu

builder.EnableLowerCamelCase()

source