【问题标题】:Pass querystring with object to WebAPI?将带有对象的查询字符串传递给WebAPI?
【发布时间】:2019-06-12 00:58:12
【问题描述】:

是否可以将查询字符串和模型对象从 angular 传递到 webapi?我的第一个参数总是被填充,但模型参数为空,即使我看到请求有效负载中的所有数据。

我的模型

public Class Person
{
   public string Name {get; set;}
   public DateTime date {get; set;}
}

我的 API 方法

    [Route("MyRoute/")]
    [HttpPost]       
    public DataSourceResult DataResult([ModelBinder(typeof(WebApiDataSourceRequestModelBinder))]DataSourceRequest request, Person model)
    {          

    }

我的查询字符串

http://localhost:60655/api/DataInput/DataResult/?page=1&pageSize=22

请求负载

model: {name: "Time", date: "2014-12-18T18:35:52.087Z"…}

我的帖子调用

const queryStr = `${toDataSourceRequestString(state)}`;
const ComplexObj = {
      model: model,     
    };
    const url = this._srvrUrl + apiRoute;    
    return this._http
      .post(`${url}?${queryStr}`, ComplexObj)

【问题讨论】:

    标签: angular asp.net-web-api kendo-ui


    【解决方案1】:

    您应该可以只使用[FromUri],例如:

    [Route("MyRoute/")]
    [HttpPost]       
    public DataSourceResult DataResult([FromUri] DataSourceRequest request, Person model)
    {          
    
    }
    

    请参阅已回答的这个非常相似的问题:Complex type is getting null in a ApiController parameter

    【讨论】:

      【解决方案2】:

      很不清楚你在问什么,还有很多不相关的代码。我正在回答你的标题问题,但身体并没有帮助我帮助你。

      使用 Angular HttpClient,您可以将查询字符串作为 Http 参数传递。

      这是一个调用 api 并返回 observable 的方法:

      method(object: any) {
        const body = JSON.stringify(object); // Create body object
        const headers = new HttpHeaders({'Content-Type': 'application/json'}); // Create headers
        const param = "some-param"; // Create param value
        return this.http.post<IResponse>('someurl.com/some/path', body, {headers: headers, params: new HttpParams().set('param name', param )});
      } 
      

      【讨论】:

      • 如何在 WebApi 中检索一次参数
      猜你喜欢
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多