0
我有一個包含動態特性,簡單的開放型:C#的OData v4的開放型原始值
public class Person {
[Key]
public string Uid { get; set; }
public IDictionary<string, object> DynamicProperties { get; set; }
}
在我的控制,我填的這些特性是這樣的:
public IHttpActionResult Get([FromODataUri] string key) {
Person person;
// ...
person.DynamicProperties.Add("foo", "foo_value");
person.DynamicProperties.Add("bar", "bar_value");
// ...
return this.Ok(person);
}
我可以計算如何通過一個類似〜/ Person('uid')/ foo的URL來請求這些屬性 - 感謝他們的密鑰和此方法:
public IHttpActionResult GetDynamicProperty([FromODataUri] string key, [FromODataUri] string dynamicProperty) {
Person person;
// ...
// (string) for the example
return this.Ok((string)person.DynamicProperties[dynamicProperty]);
}
結果是這樣的:
{
"@odata.context": "http://myServer/$metadata#Person('uid')/foo",
"value": "foo_value"
}
但我想不出如何獲取這些屬性的原始值,與像〜/人(「UID」)/富/ $值的URL
如果我嘗試一下,結果是這樣的:
{
"error": {
"code": "",
"message": "No HTTP resource was found that matches the request URI 'http://myServer/Person('uid')/foo/$value'.",
"innererror": {
"message": "No routing convention was found to select an action for the OData path with template '~/entityset/key/dynamicproperty/$value'.",
"type": "",
"stacktrace": ""
}
}
}
我該如何路由以訪問這些原始值?還是有一種方法或其他方法可以處理它?
- Sam Xu的回答
感謝您的例子後編輯,它就像一個魅力。我已經按照你的點1和2點3個樣品,如果它可以幫助別人,這是一個典型的Web API的註冊方法的改編版:
public static void Register(HttpConfiguration config) {
// Custom routing for dynamic attributes raw values
var routings = ODataRoutingConventions.CreateDefault();
routings.Insert(0, new DynamicRawValueRoutingConvention());
// Our model
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>("Person");
// ...
// Old way
//config.MapODataServiceRoute(
// routeName: "myDefaultRouteName",
// routePrefix: null,
// model: builder.GetEdmModel()
//);
// New way with Sam's samples
config.MapODataServiceRoute(
"myDefaultRouteName",
null,
configureAction =>
configureAction.AddService(ServiceLifetime.Singleton, sp => builder.GetEdmModel()).AddService<IEnumerable<IODataRoutingConvention>>(ServiceLifetime.Singleton, sp => routings)
);
var formatters = ODataMediaTypeFormatters.Create();
foreach (var oDataMediaTypeFormatter in formatters) {
oDataMediaTypeFormatter.MediaTypeMappings.Insert(0, new ODataDynamicValueMediaTypeMapping());
}
config.Formatters.InsertRange(0, formatters);
}
會'〜/人( 'UID')?$選擇= foo'是否足夠呢? – GWigWam
不幸的是沒有,因爲除非技術上不可行,否則(我無法回答這個問題),客戶期望使用非經典動態屬性的「經典」語法 –