Developer forum

Forum » Development » WebAPI in Dynamicweb

WebAPI in Dynamicweb

Mikkel Toustrup Olsen
Reply
Hello, A fellow developer asked for the possibility of implementing WebAPI in Dynamicweb - see url below. http://developer.dynamicweb.com/forum/development/how-do-i-implement-webapi-in-dw.aspx Is there no documentation on this whatsoever? :) - It would be great if you guys could point me / us in the right direction. Regards, MikkelTO

Replies

 
Klavs Martens
Reply
This post has been marked as an answer

Hi Mikkel

We don't have any specific documentation on MVC in Dynamciweb, but here is the code for my presentation from Tech Conf 2014. I contains a WebAPI contrroller. Look in the Controller  folder and Global.asax to folder to find the code for a WebAPI controller and in WebApiConfig.cs to find a sample Route configuration. If you need access to any Dynamicweb API's that requires an active session, you must use RouteHandler = new SessionRouteHandler() in you route configuration and make you client resend the ASP.NET session cookie on every request. If you're calling you own API, then you dont need to worry about sessions.

You'll need a custom solution to get it to work.

Klavs

Votes for this answer: 1
 
Mikkel Toustrup Olsen
Reply

Thank you so much Klavs :-)

Best Regards,

MikkelTO

 
António Ramos
Reply

https://vimeo.com/91691704

 
Jakob -
Reply

If i am only implementing Webapi, can you tell me why i dont have context on getting my cart back, i build my test project up as your MVC build and left MVC out am i needing something to get the context ? 

I have added a new SessionRouteHandler, but that doesnt seem to do the trick ?

http://puu.sh/ipxsc/538c776f67.png

EDIT : Abit code, my cartContext keeps being empty is it just me being a newbie at dynamicweb or dont i have content if cartContext = null ? 

I have done the same on my product controller and gets context of the language id, is this just falling back to default or do i have context ? 

http://puu.sh/ipxxl/ec3433539e.png

 

Cheers Jakob Kristensen

 
Klavs Martens
Reply

Hi Jakob

Make sure your client resend the ASP.NET session cookie on every request.

Klavs

 
Jakob -
Reply

Hi Klavs

Thanks for the fast answer, when you say resend the asp.net session do you mean from global.asax Application_BeginRequest or Session_Start ?

If you have some sample code on this subject i would love to see it :)

Edit: i found something like this http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api

I need to resend the session would i resend it as these guys does, or is it some specific implementation ?

Thanks 
Jakob Kristensen

 
Nicolai Høeg Pedersen
Reply

Hi Jakob

Like the article you found. It is just session...

 

 
Jakob -
Reply

Hello again.

I've done some more research on the matter and it seemed i was missing a cookie, i then put it up on the server and the cookie is there and the session is valid.

The cart will just return null, and i wont get any data back, any ideas on how to fix this, i am running out of options.

 

And please can you clarify what you mean with resend the asp.net session cookie on every request, i thought that is what the new SessionRouteHandler(); was for?

Mind you the api is working perfect without sessions so i have access to dynamicweb and the database, its just the context cart that is bugging me out.

Some code for getting a cart and throw it to the api, what am i doing wrong here ?

     public IEnumerable<ApiOrderLine> Get()
        {
            var cart = Dynamicweb.eCommerce.Common.Context.Cart;
            
            if (cart != null)
                foreach (var lines in cart.OrderLines)
                {
                    var orderline = new ApiOrderLine();
                    orderline.Price = lines.Price.PriceWithVATFormatted;
                    orderline.Id = lines.ID;
                    orderline.ProductName = lines.ProductName;
                    GetCart.Add(orderline);
                }

            return GetCart;
        }

 

Cookie screenshot to show i have the cookie.

http://puu.sh/iqKt9/65be72879a.png

 

Debugging further on this: 

So i found it interesting that i could actually get a current.user of my session (Dynamicweb.Modules.UserManagement.User.GetCurrentUser()), could this be a problem with the api where you get the cart ? (Dynamicweb.eCommerce.Common.Context.Cart) These should both need access to session so my session seems valid, but not when i am getting the cart ?

Cheers Jakob Kristensen

 

You must be logged in to post in the forum