Developer forum

Forum » Development » Custom WebApi controller not triggered

Custom WebApi controller not triggered

Martin Moen
Reply

I'm going blind, and a bit crazy over what should have been a quite easy task.
Trying to add a custom WebApi to my Dynamicweb 9.13.4 solution, but it just wont work..

This is what I got. The source code is attached.

WebApiEnabler.cs

Controllers/GeneralController.cs

This just show a 404 if I try to open the page.

Also, no message is being logged through the LogManager.
However, if I comment out everything in GeneralController.cs I do get log messages in the event viewer.

Looks like the notification subscriber is not been hit whenever I use my controller code.

What am I missing?! 


Replies

 
Nicolai Pedersen
Nicolai Pedersen
Reply

Try to use

var request = authenticateArgs.Application.Context.Request;

Instead of httpcontext.current. This is so early in the request that current.request is not set.

BR Nicolai

 
Martin Moen
Reply

Then I get:

'IApplication' does not contain a definition for 'Context' and no accessible extension method 'Context' accepting a first argument of type 'IApplication' could be found
 
Nicolai Pedersen
Nicolai Pedersen
Reply

Ah - it is private!

Try with Dynamicweb.Context.Current.Request

 
Nicolai Pedersen
Nicolai Pedersen
Reply

And check that request.path has a value!

 
Martin Moen
Reply

Still the same result. It really does not look like the WebApiEnabler is the problem.
Whenever I comment out all the code in GeneralController.cs:

I do get this in my event viewer:

Mening that this code, has been triggered:

When I uncomment the code in GeneralController.cs I do not get any events in my event viewer...

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Your browser shows localhost:9001 yet your logs image shows a DW machine called DW-D026. Are you sure you are testing the correct thing?

Imar

 
Martin Moen
Reply

Yes, I get the same result in localhost. I just tested it quickly on another solution earlier today. But that has nothing to do with the real issue here.
I get the same result on both localhost and hosted solutions.

 
Martin Moen
Reply

But does it look like my code (full project attached in main post) is correct?
Do any of you make it work on your local solutions?

 
Nicholai West Pedersen Dynamicweb Employee
Nicholai West Pedersen
Reply

Hi Martin,

Have you checked if your notification subscriber 'WebAPiEnabler.cs' is present in the list of notification subscribers? If it isn't, that may be why the notification subscriber does not get hit

 
Nicholai West Pedersen Dynamicweb Employee
Nicholai West Pedersen
Reply

I'm currently trying to see if I can figure out why it is not working, by using your code from the attachment. I'll return with more info as soon as I've had a look at it

/N.W.P.

 
Nicholai West Pedersen Dynamicweb Employee
Nicholai West Pedersen
Reply
This post has been marked as an answer

I got it to work on my local solution with the following code:

What I noticed from my testing was, that whether the notification subscriber shows up in the list of notification subscribers or not seems to be connected to: the type of project, the version and which packages are used in the solution (assuming the code looks ok). I'm not 100% sure yet what exactly causes the notificaiton subscriber not to be shown when a controller is present (without debugging the source code) but I got it to work by creating a new Class library (.NET Framework - version 4.7.2) and adding packages to my newly created solution from a working custom webapi's packages.config file. I've attached my working project in case you want to have a look at it 

https://www.screencast.com/t/DFXOH98Gd

 

Votes for this answer: 1
 
Martin Moen
Reply

Thanks Nicholai!

I can confirm that it is working here too. But it does not look like the code is the issue.
Tried to copy the same code as you had into my project, but it did not work.

When I added your prosject into my solution it worked as expected. Think there is something with how the project was created in Visual Studio.
I was missing a few files and folders which you had in your project.

Anyway, it is working. Thanks for your time helping me out :)

 
Nicholai West Pedersen Dynamicweb Employee
Nicholai West Pedersen
Reply

You're welcome :)

Yes, the issue here seems to be related to the project-setup and the packages used in the solution 

 

You must be logged in to post in the forum