Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to replace the IHttpControllerSelector service? #2494


User avatar
0
mgx92 created

Hello community,

I want to replace the web api IHttpControllerSelector service, because I want to use versioning via media types. But when I try it like this:

Configuration.Modules.AbpWebApi().HttpConfiguration.Services.Replace(typeof(IHttpControllerSelector),
                new AcceptHeaderControllerSelector(Configuration.Modules.AbpWebApi().HttpConfiguration,
                accept =>
                {
                    foreach (var parameter in accept.Parameters)
                    {
                        if (parameter.Name.Equals("version", System.StringComparison.InvariantCultureIgnoreCase))
                        {
                            switch (parameter.Value)
                            {
                                case "1.0": return "v1";
                            }
                        }
                    }
                    return "v1";
                }));

The new AcceptHeaderControllerSelector never called. I don't know what's wrong with this solution.. maybe I have to do it via Configuration.ServiceReplace?

I hope someone can help me.

mxg


2 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Your way seems correct. Where do you run this code ? Is it in the PostInitialize of your webapi module ?

  • User Avatar
    0
    mgx92 created

    Oh no, I run this code in the Initialize method... thank you very much - your suggestion works! I put the code into the postInitialize and it works! :)