Asp.net Webapi使用[FromUri]和Route有两个参数

时间:2016-01-24 10:11:21

标签: c# asp.net-web-api asp.net-web-api-routing

   [Route("Street/{ZoneID}/{StreetID}/")]
    public HttpResponseMessage GetStreet(int ZoneID,int StreetID,[FromUri]       RealEstateFilter Filter)

当从客户端发送以下请求时,StreetID始终为0

  

http://localhost:1887/Street/34/23295?MunZone=7&StartDate=&EndDate=

但它没有任何可选参数

  

http://localhost:1887/Street/34/23295

我应该如何配置webapi以便能够阅读[Fromuri]

1 个答案:

答案 0 :(得分:6)

使用我的控制器调用Street/34/23295?MunZone=7&StartDate=&EndDate=定义为:

public class SomeController : ApiController
{
    [Route("Street/{ZoneID}/{StreetID}/")]
    public HttpResponseMessage GetStreet(int ZoneID, int StreetID, [FromUri] RealEstateFilter Filter)
    {
        return null;
    }

都给:

enter image description here

当我将RealEstateFilter定义为:

public class RealEstateFilter
{
    public int? MunZone { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
}

我的web.api版本是:

  <package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net451" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net451" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net451" developmentDependency="true" />

我从头开始创建我的Web.Api项目,这是唯一的东西。您是否可能有其他干扰途径?