Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

EPiServer Commerce 7.5 with EPiServer Integration and Web Api 2

Vote:
 

Hi

Trying to get our own Web Api 2 controllers to work within a solution with EPiServer Commerce and the EPiserver Web Api 2 based integration. the Integration which has the route of episerverapi/ works since it's used in an Perfion integration but how do I get my own api to work?

Following these articles:

http://world.episerver.com/Blogs/Linus-Ekstrom/Dates/2014/7/Built-in-auto-suggestion-editor/

http://www.dcaric.com/blog/episerver-login-functionality-using-web-api-2

I have a basic Controller setup and when I try to register attributerouting in Global.asax.cs like this:

GlobalConfiguration.Configure(WebApiConfig.Register);

I get the following error

[ArgumentException: A route named 'MS_attributerouteWebApi' is already in the route collection. Route names must be unique.
Parameter name: name]
   System.Web.Routing.RouteCollection.Add(String name, RouteBase item) +3707045
   System.Web.Http.Routing.AttributeRoutingMapper.MapAttributeRoutes(HttpConfiguration configuration, IInlineConstraintResolver constraintResolver) +144
   WebApplication.WebApiConfig.Register(HttpConfiguration config) in c:\Workspace\FlaktWoods\FlaktWoods.Web\Business\Initialization\WebApiConfig.cs:10
   System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +65

But if I leave out the registration from Global.asax.cs my routes does not work. Made a simple test controller method like this:

[RoutePrefix("commerceapi")]
    public class CommerceApiController : ApiController
    {

        [Route("status")]
        [HttpGet]
        public string Status()
        {
            return "Hello!";
        }
    }

But all I get is a 404 when calling http://localhost/commerceapi/status. Is it some registration I'm missing and how does this work when the EPiServer.Integration is installed?

#109797
Oct 15, 2014 11:07
Vote:
 

My thought is this should be a Web API configuration, instead of Commerce.

What's your Global.asax.cs looks like, at least the Application_Start method?

/Q

#109800
Oct 15, 2014 12:12
Vote:
 

The Global.asax.cs is triggering GlobalConfiguration.Configure(WebApiConfig.Register); in it's Application_Start

protected void Application_Start()
        {
            
            
            // Register Display Options
            var options = ServiceLocator.Current.GetInstance<DisplayOptions>();
            options
                .Add("full", "/displayoptions/full", ContentAreaTags.FullWidth, "", "epi-icon__layout--full")
                .Add("wide", "/displayoptions/wide", ContentAreaTags.TwoThirdsWidth, "", "epi-icon__layout--two-thirds")
                .Add("narrow", "/displayoptions/narrow", ContentAreaTags.OneThirdWidth, "", "epi-icon__layout--one-third");

            AreaRegistration.RegisterAllAreas();
            GlobalConfiguration.Configure(WebApiConfig.Register);
        }

And the line causing the error is the config.MapHttpAttributeRoutes(); in WebApiConfig:

using System.Web.Http;

namespace WebApplication
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

            // Other Web API configuration not shown.
        }
    }
}

This is just a class I created following the articles above but since we use the integration it seems like this simple way doesn't work. Tried to change the RoutePrefix to "api/commerce" just in case the "commerceapi" I set wasn't causing the issue but still same result

#109812
Oct 15, 2014 13:30
Vote:
 

Seem I'm getting a littel bit closer to the solution by doing the following changes:

In the WebApiConfig I removed the  config.MapHttpAttributeRoutes() and instead added a registration of the route I want to use:

public static void Register(HttpConfiguration config)
        {
            // Web API routes
            //config.MapHttpAttributeRoutes();

            // Other Web API configuration not shown.
            config.Routes.MapHttpRoute(
                name: "CommerceApi",
                routeTemplate: "api/{controller}/{action}");
        }

Changing the Controller to this:

[RoutePrefix("api/commerce")]
    public class CommerceApiController : ApiController
    {
        [Route("status")]
        [HttpGet]
        public IHttpActionResult Status()
        {
            return Ok();
        }
    }

Now I don't get the standard 404, instead I'm presented with the following XML result:

<Error>
<Message>No HTTP resource was found that matches the request URI 'http://localhost/api/commerce/status/'.
</Message>
<MessageDetail>No type was found that matches the controller named 'commerce'.
</MessageDetail>
</Error>

Have tried some different route settings but I can't get it to function correctly

#109826
Oct 15, 2014 14:55
Vote:
 

Efter some more researching I'm back to square one. This since the custom routes specified in WebApiConfig is not for Attribute routing and this way of specifying routes will not find controllers specified by routes or based on the APiController.

So now I'm stuck with not being able to call the 

config.MapHttpAttributeRoutes();

without causing the  'MS_attributerouteWebApi' error.

#111680
Oct 17, 2014 13:32
Vote:
 

Nudging this - I too am having great difficulty getting WebAPI to work in an EPiServer, Commerce & EPiFind project.

Did you get anywhere?

The following article allowed me to see the routes in the RouteTable and I never see any of my attributed ones! 

My gut is telling me something in Commerce or Find is doing something to register routes before my code runs.

#141641
Nov 17, 2015 11:41
Vote:
 

No after we spent some time trying to get this to work we dropped it and handled the Actions on specific PageControllers. It was a while ago and haven't looked at it again I'm afraid.

#141643
Nov 17, 2015 11:44
Vote:
 

Thanks for letting me know Tobais,

Im not too far off that point myself but both the Quicksilver reference implementation from EPiServer and the Commerce Starter Kit from Oxxas have a WebAPI integration (one with route attribution and one without). So I hopeful it can work!.

I've bben hacking around so long I cant tell whats going on - but right now neither approach is working.

I even tried on a new, blank EPiServer project (no add-ons, latest version - 9.x) and the route attribution worked flawlessy :(

There is something not right in my project!

#141644
Nov 17, 2015 11:49
* 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.