猿问

基于可选参数的访问控制器

我有一个现有的 WebAPI 控制器方法。


 [SwaggerDefaultResponse]

 [HttpGet, Route("{id:int}", Name = "GetDataId")]

 public IHttpActionResult Get(long id)

  { 

      //This method returns a full Data

  }

我必须编写精细的 WebAPI。我们计划有一条如下所示的路线:


/V1/MYdATA/{DataId}?DataBasic=true

如果DataBasic=true 那么我应该调用一个新方法来获取“基本数据”---并且如果DataBasic=false那么应该返回我现有的完整数据,即应该调用现有方法。


我可以了解如何在代码中实现这一点吗?


白板的微信
浏览 89回答 1
1回答

慕标琳琳

同一控制器public IHttpActionResult Get(long id, bool DataBasic){&nbsp;&nbsp; &nbsp; if (!DataBasic)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //This method returns a full Data&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;//Method Retrieving new data&nbsp; &nbsp; }}不同的控制器@using (Html.BeginForm(model.Databasic ? "GetNew" : "GetFull", "ControllerName", new {id = model.Id})){&nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" name="Get" />}public IHttpActionResult GetNew(long? id){&nbsp;&nbsp; &nbsp; //Method Retrieving new data}public IHttpActionResult GetFull(long id){&nbsp;&nbsp; &nbsp; //This method returns a full Data}
随时随地看视频慕课网APP
我要回答