November Happy Hour will be moved to Thursday December 5th.

Web API attribute routing not working

Vote:
 

Hi,

I have a problem that I have been struggling with for quite some time now.
I am trying to implement a web api to our project, that should work much like episervers service api.
I've made it so far that I can get request a token, but I can't get my attribute routings to work.

What makes me confused is that I have pretty much copy/pasted episervers implemention of their service api (which works), but mine won't.
Obvisously I am missing something, or is there some kinda magic behind episervers attribute routes?

Here is the code for my Web API project:

ApplicationBuilderExtensions.cs

public static class ApplicationBuilderExtensions
{
    public static IAppBuilder UseServiceApi(this IAppBuilder app) where TManager : UserManager where TUser : class, IUser
    {
        var options = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/serviceapi/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(20),
            Provider = new IdentityAuthorizationServerProvider()
        };
            
        app.UseOAuthAuthorizationServer(options);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
            
        return app;
    }
}


IntegrationInitialization.cs

[InitializableModule, ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class IntegrationInitialization : IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        GlobalConfiguration.Configure(config =>
        {
            var current = (IHostBufferPolicySelector)config.Services.GetService(typeof(IHostBufferPolicySelector));

            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
            config.Services.Replace(typeof(IHostBufferPolicySelector), new StreamingInputPolicy(current));
            config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings();
            config.Formatters.XmlFormatter.UseXmlSerializer = true;
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        });
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

BrandsController.cs

[RoutePrefix("serviceapi/rest")]
public class BrandsController : ApiController
{
    [HttpGet]
    [ResponseType(typeof(IEnumerable))]
    [Route("brands")]
    public virtual IHttpActionResult GetBrands()
    {
        var brands = new List();
        brands.Add(new Brand() { Code = "hej" });

        return Ok(brands);
    }
}

And this is Startup.cs in our Main project

app.UseServiceApi, SiteUser>(); // My own web api
app.UseServiceApiIdentityTokenAuthorization, SiteUser>(); // Episerver service api


And then we have a initialization module in our Main project that map the attribute routes

[InitializableModule]
[ModuleDependency(typeof(EPiServer.Commerce.Initialization.InitializationModule))]
public class SiteInitialization : IConfigurableModule, IInitializableModule
{
    public void Initialize(InitializationEngine context)
    {
        GlobalFilters.Filters.Add(new HandleErrorAttribute());
        AreaRegistration.RegisterAllAreas();
    }

    public void ConfigureContainer(ServiceConfigurationContext context)
    {
        DependencyResolver.SetResolver(new StructureMapDependencyResolver(context.StructureMap()));
        GlobalConfiguration.Configure(config =>
        {
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;
            config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings();
            config.Formatters.XmlFormatter.UseXmlSerializer = true;
            config.DependencyResolver = new StructureMapResolver(context.StructureMap());
            config.MapHttpAttributeRoutes();
        });
    }

    public void Uninitialize(InitializationEngine context)
    {
    }
}

I am running on Episerver 10.10.4.0 and Episerver.Commerce 11.4.1.0
If you're missing some code, feel free to ask and I will update.

Thank you!

/ Kristoffer

#188013
Feb 08, 2018 13:17
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.