Replies: 2 comments
-
Support "/data({key1},{key2})", you need some codes as below:using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.OData.Extensions;
using Microsoft.AspNetCore.OData.Routing;
using Microsoft.AspNetCore.OData.Routing.Template;
using Microsoft.OData.Edm;
using ODataSample.V8;
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class ODataRouteAttribute : Attribute, IActionModelConvention
{
public void Apply(ActionModel action)
{
// Lower order value gets higher priority.
// So, Let's specify a lower order value for existing routings
// And Move the more generic routing behind.
// Without this setting.
// The request like 'http://localhost:5296/Data(Key1=2,Key2='A')' will be handled by
// routing as "/Data({key1},{key2}). And we will get exception.
foreach (var selector in action.Selectors)
{
if (selector.EndpointMetadata.Any(c => c is IODataRoutingMetadata))
{
if (selector.AttributeRouteModel != null)
{
selector.AttributeRouteModel.Order = -1;
}
}
}
IEdmEntitySet data = ModelProvider.Model.EntityContainer.FindEntitySet("Data");
IEdmEntityType dataType = data.EntityType();
IDictionary<string, string> keyMappings = new Dictionary<string, string>
{
{ "Key1", "{Key1}" },
{ "Key2", "{Key2}" }
};
action.AddSelector("GET", string.Empty,
ModelProvider.Model,
new ODataPathTemplate(new EntitySetSegmentTemplate(data),
new MyKeyTemplate(keyMappings, dataType, data)),
null);
}
}
public class MyKeyTemplate : KeySegmentTemplate
{
public MyKeyTemplate(IDictionary<string, string> keys, IEdmEntityType entityType, IEdmNavigationSource navigationSource)
: base(keys, entityType, navigationSource)
{
}
public override IEnumerable<string> GetTemplates(ODataRouteOptions options)
{
yield return "({Key1},{Key2})";
yield return "({Key1}, {Key2})";
// add more
}
} Use the attribute on the controller:Other new added lines are used to support other templates. move the model builder into a static to re-use:Here's the test |
Beta Was this translation helpful? Give feedback.
0 replies
-
@xuzhg Thank you! Your sample is so excellent and help me much! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi, guys!
I'm planning to update OData lib from v7.x to v8.x. So, I'm checking differences of the versions.
And I found there are differences at behavior of Routing.
This is my test code and the result.
v7.x succeeded all
InlineData
but x8.x failed except one which was specified atRoute
attribute.Full code of the sample is here.
https://github.com/iwate/odatasample
I'm looking for a way to allow these request on v8.x.
Do you have any idea?
This is a simple idea. Making actions as need as it.
https://github.com/iwate/odatasample/blob/999575a9f6e2f4f665405b679d7bf6f9ec9208fe/ODataSample.V8/Controllers/SampleController.cs#L24-L42
If you have another idea, could you share here?
Beta Was this translation helpful? Give feedback.
All reactions