Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • JakobJohansen 5 posts 97 karma points
    Sep 24, 2016 @ 15:23
    JakobJohansen
    0

    Using Entity Framework to peform CRUD operations on custom table in the Umbraco database

    Hello guys!

    I'm attempting to use Entity Framework to perform CRUD operations on a custom table in my Umbraco database. Here's what I've attempted so far:

    I've created a database and added a table to it called Emails with two fields - the id field and a string for the email. Then I created a new solution in Visual Studio and successfully installed Umbraco (through the nuget package manager console) into my newly created database.

    I then added an ADO.NET Entity Data Model to my models folder picking EF Designer from database (this all went well).

    I then added a controller called EmailsController (Web API 2 Controller with actions, using Entity Framework) to my controllers folder. This, seemingly, went well too. I then changed the controller to inherit from UmbracoApiController and added the necessary usings.

    I then ran my application (everything is building and running fine) and now I expected to be able to call the api (I'm doing this from a Chrome extension called Postman) and have all emails returned (I have already put some data into the table). As my controller is inheriting from the UmbracoApiController I'm expecting the url to be:

    http://localhost:1518/umbraco/api/Emails/

    However, when I call this url from Postman or the browser I get "404 - Resource not found". Am I doing something wrong? Can somebody help me debug my problem (or do you maybe already know what's wrong)? Can an Entity Framework controller not inherit from UmbracoApiController? Is my routing wrong?

    If I just have my controller inherit from the regular ApiController I'm still not able to get any return (here I'm assuming the url would be http://localhost:1518/api/Emails).

    I only have limited experience with both the Umbraco controllers and Entity Framework, so I haven't been able to debug this very well myself.

    I hope some of you can help me :-)

    Cheers, Jakob

  • Marc Goodson 2149 posts 14377 karma points MVP 9x c-trib
    Sep 25, 2016 @ 09:44
    Marc Goodson
    100

    Hi Jakob

    I tend to use PetaPoco to update and pull data from custom tables within Umbraco (because Umbraco already has a PetaPoco context available to use)

    However it seems more like a routing issue you have, rather than the data access layer!

    I'm wondering if it's because

    http://localhost:1518/umbraco/api/Emails/

    isn't routing to a particular default action in the api controller or whether you are expecting a POST or a GET verb to be sent...

    So if you have a [HTTPGET] action in your api controller called AddEmail, can you access it via

    http://localhost:1518/umbraco/api/Emails/[email protected] ??

    Otherwise I'd try creating a blank new WebApiController and make it inherit from UmbracoWebApiController and determine if you can just make the url routing work in a simple scenario before then adding the complexity of the generated entity framework controller, it might indicate the issue at hand.

    regards

    Marc

  • JakobJohansen 5 posts 97 karma points
    Sep 29, 2016 @ 12:38
    JakobJohansen
    0

    Hi Marc

    Thank you very much for your reply.

    You are right that the routing was at fault. It was my understanding that you did not need to specify an action when calling an api controller, and that it depended on the type of request instead. But when I called my controller using the action at the end of the url I got the response I had originally anticipated.

  • Veronica Burd 76 posts 201 karma points
    Sep 25, 2016 @ 11:18
    Veronica Burd
    0

    Umbraco expects all controllers derived from UmbracoApiController to have a name with the following format:

    MyNameApiController
    

    For example, I have a controller called PetitionPublicApiController with a method called CreatePetition. The correct url to access this is:

    /umbraco/Api/PetitionPublicApi/CreatePetition
    

    Note I do not include the Controller suffix in the url. Umbraco will correctly route all calls to my PetitionPublicApiController automatically.

    HTH

    Ver

Please Sign in or register to post replies

Write your reply to:

Draft