2013-06-26 48 views
0

我已經從ApiController衍生的的WebAPI控制器,其具有服務操作:加入手動定義的服務操作內部initService爲OData服務

public class Airports2Controller : ApiController 
    { 
     protected airportEntities db = new airportEntities(); 

     [Queryable] 
     [HttpGet] 
     public IQueryable<Airport> GetAirportsWithinRadius(int airportId, int radius) 
     { 
      //var radius = (int)parameters["radius"]; 
      //var airportId = (int)parameters["airportId"]; 

      var resultAirports = GetAirportsWithinRadius2(airportId, radius); 

      return resultAirports;    
     } 


     private IQueryable<Airport> GetAirportsWithinRadius2(int airportId, int radius) 
     { 
      var airports = db.Airports.SqlQuery("select * from Airport a where (select GeoLocation from airport where Id = @p0).STDistance(a.GeoLocation)/1.852/1000.00 < @p1", airportId, radius); 

      var airportIds = airports.Select(a => a.Id); 
      var resultAirports = db.Airports.Where(a => airportIds.Contains(a.Id)); 

      return resultAirports; 
     } 
    } 

我也有機場實體OData服務。由於.net webapi odata還不支持odata函數(服務操作?),所以我需要創建一個輔助控制器(這不是從ODataController派生的)。

我想現在jaydata做的是擴大的背景下,以具有除了OData的東西,服務操作,一旦數據庫被初始化在initService:

$data.initService("http://localhost:2663/odata"); 
      service.then(function (db) { 
     //now here manually extend the definition of the context to include the GetAirportsWithinRadius service operation. 
}); 

}

這控制器很好地工作得到參數,並返回正確的json手動從fiddler調用。如何擴展jaydata上下文以獲得GetAirportsWithinRadius(airportId,radius)方法?它的url需要手動設置,其類型將需要更改爲GET。此外,該方法是否可與odata params組合,因爲它是用[Queryable]聲明的。再次,手動調用fiddler時,它的一部分工作。例如:

http://localhost:2663/api/Airports2/GetAirportsWithinRadius?airportId=2112&radius=50&?$inlinecount=allpages&$top=2 

這將返回兩個機場的實體對象的很好...

感謝

+0

嘿,是有可能發佈你的邏輯作爲Web API的OData行動?您可以調用實體集級別和實體級別的操作-http://jaydata.org/blog/calling-odata-actions-and-service-operations-with-jaydata – Robesz

回答

1

感謝@robesz - 有在.NET創建參數OData的一些行動的細微之處,這就是有一直困擾着我(我曾嘗試操作之前,只是無法得到它的工作),但是這一次我得到了它:

[Queryable] 
     [HttpPost] 
     public IQueryable<Airport> GetAirportsWithinRadius([FromBody] ODataActionParameters parameters) 
     { 
      if (!ModelState.IsValid) 
      { 
       throw new HttpResponseException(HttpStatusCode.BadRequest); 
      } 
      var radius = (int)parameters["radius"]; 
      var airportId = (int)parameters["airportId"]; 

      var resultAirports = GetAirportsWithinRadius2(airportId, radius); 

      return resultAirports;    
     } 

ActionConfiguration getAirportsWithinRadius = modelBuilder.Entity<Airport>().Collection.Action("GetAirportsWithinRadius"); 
      getAirportsWithinRadius.Parameter<int>("airportId"); 
      getAirportsWithinRadius.Parameter<int>("radius"); 
      getAirportsWithinRadius.ReturnsCollectionFromEntitySet<Airport>("Airports"); 

有了這個地方,我現在可以做的後續荷蘭國際集團與jaydata:

var service = $data.initService("http://localhost:2663/odata"); 
     return service.then(function (db) { 
      airports = db.Airports.GetAirportsWithinRadius(2112,50); 
      airports.filter("it.Abbrev== a", {a: 'C44'}).forEach(function(a){console.log(a.Abbrev)}); 
    }); 

我在快樂:)上躥下跳

+0

哇!真棒,恭喜! :) – Robesz