【问题标题】:How do you format a GET uri that uses nested complex objects?如何格式化使用嵌套复杂对象的 GET uri?
【发布时间】:2016-01-22 00:14:35
【问题描述】:

我有以下 Get API

public class FooController
{
    public IHttpActionResult Get([FromUri] FooCriteria criteria) { ... }
}

public class FooCriteria
{
    public string Baz { get; set; }

    public Location Location { get; set; }
}

public class Location
{
    public double Latitude { get; set; }

    public double Longitude { get; set; }
}

我一生都无法弄清楚 Uri 的格式以正确传递嵌套的复杂类型。

到目前为止,我有http://localhost:8957/api/Foo/?Baz=hello& WHAT GOES HERE

【问题讨论】:

    标签: c# asp.net-web-api get uri complextype


    【解决方案1】:

    这应该是你的网址;

    http://localhost:8957/api/Foo/Get?Baz=ThisIsBaz&Location=WebApi.Controllers.FooController%2BLocation

    像这样修改你的控制器;

    public class FooController : Controller
    {
        public IHttpActionResult Get(FooCriteria criteria)
        {          
            return new OkResult(new HttpRequestMessage());
        }
    
        public ActionResult Index()
        {           
            return View();
        }
    
    
        public class FooCriteria
        {
            public string Baz { get; set; }
    
            public Location Location { get; set; }
        }
    
        public class Location
        {
            public double Latitude { get; set; }
    
            public double Longitude { get; set; }
        }
    }
    

    然后创建两个视图分别称为Index和Get

    @using WebApi.Controllers
    @model WebApi.Controllers.FooController.FooCriteria
    
    @{
        ViewBag.Title = "title";
    }
    <h2>title</h2>
    
    
    @Html.ActionLink("Your GET", "Get", new FooController.FooCriteria(){Baz="ThisIsBaz", Location = new FooController.Location(){Latitude = 10, Longitude = 20}})
    

    现在,当您运行时,您应该能够看到 ActionLink 中 URL 中传递的内容

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 2022-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-29
      • 2011-10-08
      相关资源
      最近更新 更多