Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Multiple Models with Attribute Routing #220

Closed
abombss opened this issue Feb 11, 2015 · 1 comment
Closed

Support Multiple Models with Attribute Routing #220

abombss opened this issue Feb 11, 2015 · 1 comment
Milestone

Comments

@abombss
Copy link

abombss commented Feb 11, 2015

When using OData Attribute Routing and the Attribute Routing Convention a single host cannot handle multiple EDM models.

I am not sure the best solution to resolve this, here is the work around we came up with to solve this.

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
    public sealed class ODataModelAttribute : Attribute
    {
        public ODataModelAttribute(string containerFullName)
        {
            ContainerFullName = containerFullName;
        }

        public string ContainerFullName { get; private set; }
    }

    public class ModelBoundODataAttributeRoutingConvention : AttributeRoutingConvention
    {
        public override bool ShouldMapController(HttpControllerDescriptor controller)
        {
            if (controller == null)
            {
                return base.ShouldMapController(null);
            }

            var odataModelAttribute = controller.GetCustomAttributes<ODataModelAttribute>().FirstOrDefault();

            if (odataModelAttribute == null)
            {
                return base.ShouldMapController(controller);
            }

            // This should be case sensitive since OData is case sensitive
            return odataModelAttribute.ContainerFullName.Equals(
                Model.EntityContainer.FullName(),
                StringComparison.Ordinal);
        }
    }

    public static class HttpConfigurationExtensions
    {
        public static ODataRoute MapModelBoundODataServiceRoute(
            this HttpConfiguration configuration,
            string routeName,
            string routePrefix,
            IEdmModel model)
        {
            var conventions =
                (new[] { new ModelBoundODataAttributeRoutingConvention(model, configuration) }).Concat(
                    ODataRoutingConventions.CreateDefault());

            return System.Web.OData.Extensions.HttpConfigurationExtensions.MapODataServiceRoute(
                configuration,
                routeName,
                routePrefix,
                model,
                new DefaultODataPathHandler(),
                conventions);
        }
    }

And some sample usage.

namespace Sample 
{
[ODataModel("Sample.Model1")]
public class Model1Controller : ODataController
{
}

[ODataModel("Sample.Model2")]
public class Model2Controller : ODataController
{
}
}
public static class ODataConfig
{
        public static void Register(HttpConfiguration config)
        {
            ODataModelBuilder model1Builder = new ODataConventionModelBuilder()
            {
                ContainerName = "Model1",
                Namespace = "Sample",
            }

            model1Builder.EntitySet<Model1>("Model1");

            config.MapModelBoundODataServiceRoute(
                "OData-Model1",
                "api/Model1",
                model1Builder.GetEdmModel());

            ODataModelBuilder model2Builder = new ODataConventionModelBuilder()
            {
                ContainerName = "Model2",
                Namespace = "Sample",
            }

            model1Builder.EntitySet<Model2>("Model2");

            config.MapModelBoundODataServiceRoute(
                "OData-Model2",
                "api/Model2",
                model2Builder.GetEdmModel());                      
        }
}
@congysu congysu added this to the V5.6 milestone Feb 17, 2015
@xuzhg xuzhg modified the milestones: V5.7, V5.6 May 29, 2015
@xuzhg xuzhg modified the milestones: v5.8, v5.7 Jul 2, 2015
@VikingsFan VikingsFan modified the milestones: v5.9, v5.8 Jan 6, 2016
@VikingsFan VikingsFan modified the milestones: v5.10, v5.9 Mar 21, 2016
@robward-ms robward-ms added the P4 label Jun 7, 2017
@habbes
Copy link
Contributor

habbes commented Mar 1, 2022

Closing this issue due to inactivity. If this still affects the latest versions of the library, kindly open a new issue.

@habbes habbes closed this as completed Mar 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants