English 中文(简体)
ASP. NET Web AP IQueryable<T> challenge
原标题:ASP.NET Web API IQueryable<T> challenge

我想在我的控制人员中使用以下模式:

api/{ Controller}/{id}/{ Collection}

Example:api/customers/123/addresses

但我希望回到<代码>。 IQueryable Of T from the due Get action. 我想这样作(简化):

public IQueryable<????> GetCollection(int id, string collection)  
{  
    switch(collection)  
    {  
        case "addresses":  
            return _addresses.AsQueryable();  
            break;  
        case "orders":  
            return _orders.AsQueryable();  
            break;  
        default:  
            throw new Exception(NotSupported);  
    }  
}   

Can this be done?
What would be the recommended approach?

问题回答

@S Lacks is disabilities that You should re I Queryable<object> or IQueryable<someBaseType> 如果你能够的话。

您的错误是数据用户的一种功能。 因此,你们有一些选择。

  1. Switch to an alternate xml serlializer that supports what you want.
  2. Swtitch to a form of output that bypasses the serializer at issue (say JSON using JSON.net)
  3. Teach the data contract serializer how to serialize your object using the

对于“教学”方案,你可以两种方式授课。

<>(A) 杠杆效应>[KnownType(类型......)]属性。 页: 1 <代码>KnownType> 属下。 它为世界妇联服务,但应该开始。

<>(B)使用数据合同解决器。 http://blogs.msdn.com/b/carlosfigueira/archive/09/21/wcf-extensibility-data-Treaty-resolver.aspx” rel=“nofollow noreferer” ,请你开始

扩大“Ebarr”所说的话,最容易做到这一点的办法是更新你希望能够返回的课堂,并继承共同的基类或接口。

Example:

[KnownType(typeof(Address))]
[KnownType(typeof(Order))]
public abstract class _BaseObject { }

public partial class Address : _BaseObject { }
public partial class Order : _BaseObject { }

现在,你可以采取控制者的方法:

public IQueryable<_BaseObject> GetCollection(int id, string collection) {  
    switch(collection) {  
        case "addresses":  
            return _addresses.AsQueryable();
        case "orders":  
            return _orders.AsQueryable();
        default:  
            throw new NotSupportedException();  
    }  
}

注:<代码>[KnownType]属性:System.Runtime.Serialization。 您也应当认识到,这种方法将产生您对以下编码的期待:序列化-但XML序列化,通常会导致显示您物体为基类的标签(因为你返回的物体)而不是子类。

Just return the non-generic IQueryable.
Or IQueryable<object> via covariance.





相关问题
Download file using Web API call

I am using Web API to download a file. Let me first preface that this is new territory for me. My download function inside the Web API code will initiate a download if I go directly to the API s ...

Indian Railway Train Search API [closed]

Is there any API provided by Indian Railways to search its train network, time-tables etc. There are many sites out there which show time-table etc. I searched Google but couldn t find any info on Web ...

Freely Available Web Services/APIs [closed]

I m building some Silverlight applications in my spare time, and I wanted find some freely available web services or APIs that I can call. Any suggestions?

What s the simplest way to do authentication with a web API?

I ve got a web API that provides data to users without authentication (the website lets users post data, after they ve logged in using traditional cookies & sessions). Someone wants to develop an ...

ISBN -> bookdata Lookup to fill in a database

Ok, I wanting to database a small library. I ve only had limited experience with databases, and none with querying from a webserver. I m going to want to retrieve information like title, Publisher, ...

Does IMDB provide an API? [closed]

I recently found a movie organizer application which fetches its data from the IMDB database. Does IMDB provide an API for this, or any third party APIs available?

Google Alerts web API for Python?

Google web APIs have a Python library. Google Alerts has an email and an RSS feed. Does anyone know of an automated way to add Google Alerts through a web API in Python? That is, without clicking. ...

Finding Websites From Company Name

I ve got a list of 6,000 company names (along with their headquarters address) and I need to find the web address for each of them. I m considering using the Google Web API (obviously this will take a ...

热门标签