Copied to clipboard

Flag this post as spam?

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


  • Steve 140 posts 321 karma points
    Mar 24, 2015 @ 19:27
    Steve
    0

    404 error when using WebApi

    I've read up on building a WebApi controller, including the umbraco documentation, also downloaded this example to study.

    Unfortunately, I can't seem to get anything but a 404 error when browsing ~/umbraco/api/[controllername]/[method]

    It is indicated that 'we will auto-route this controller so you don't have to worry about routing at all' in the documentation but it seems like something is not working. Any help would be greatly appreciated!

    Regards,

    Steve

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 24, 2015 @ 19:41
    Stefan Kip
    0

    What Umbraco version?
    What is the URL you are calling?
    What is the Controller definition (class)? What methods does it have?

  • Steve 140 posts 321 karma points
    Mar 24, 2015 @ 19:52
    Steve
    0

    Hi Kipuseop,

    Apologies for the lack of details.

    Using Umbraco version 7.1.3

    URL is according to the instructions in the documentation: http://localhost/umbraco/api/statusapi/getstatus?id=123

    The controller definition class is just a reduced version (all methods removed except the GetAllStatuses and GetStatus) of the StatusApiController found in the example by Tim. here it is:

    using System.Globalization;

    using System.Linq;

    using System.Web;

    using System.Web.Mvc;

    using Examine;

    using Umbraco.Core.Models;

    using Umbraco.Web;

    using Umbraco.Web.Models;

    using Umbraco.Web.Mvc;

    using Umbraco.Web.WebApi;

    using SingePageCrud.Models;

    using Umbraco.Web.WebApi.UmbracoApiController;

     

    namespace SingePageCrud.Controllers

    {

        public class StatusApiController : UmbracoApiController

        {

            private const string StatusDocTypeAlias = "umbNewsItem";

            private const string MessagePropertyAlias = "publishDate";

     

            //Fetches all status docs under the specified parent

            public IEnumerable<Status> GetAllStatuses(int parentId)

            {

                UmbracoHelper help = new UmbracoHelper(UmbracoContext);

                return help.TypedContent(parentId)

                    .Children

                    .Where(c => c.DocumentTypeAlias == StatusDocTypeAlias)

                    .Select(obj => new Status()

                    {

                        Title = obj.Name,

                        Message = obj.GetPropertyValue<string>(MessagePropertyAlias),

                        Id = obj.Id

                    });

            }

     

            //Gets a single status doc based on the id

            public Status GetStatus(int id)

            {

                UmbracoHelper help = new UmbracoHelper(UmbracoContext);

                var content = help.TypedContent(id);

                return new Status

                {

                    Id = content.Id,

                    Title = content.Name,

                    Message = content.GetPropertyValue<string>(MessagePropertyAlias)

                };

            }

     

        }

     

    }

    --

    I added only the reference to using Umbraco.Web.WebApi.UmbracoApiController;

    Steve

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Mar 24, 2015 @ 19:58
    Stefan Kip
    0

    Hmm it works for me if I just create a class called StatusApiController inheriting UmbracoApiController with the GetStatus(int id) method.
    I'd suggest to enable debug logging in the log4net.config and check the logs about what's going on...

  • Steve 140 posts 321 karma points
    Mar 24, 2015 @ 20:10
    Steve
    0

    I edited the class down to:

    using Umbraco.Web.WebApi.UmbracoApiController;

    public class StatusApiController : UmbracoApiController

        {

            //Gets a single status doc based on the id

            public Status GetStatus(int id)

            {  

            }

        }

    and still get IIS 404 error. There seems to be no activity in the log file, as if Umbraco is never even processing the request but rather IIS just looks for the resource.

    Is there a config setting somewhere to enable the controller or indicate routes for the WebApi that I've missed?

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Mar 24, 2015 @ 20:47
    Bjarne Fyrstenborg
    103

    Hi Steve

    I don't think you should include using Umbraco.Web.WebApi.UmbracoApiController; just using Umbraco.Web.WebApi; and then inherit from UmbracoApiController

    What if you just add the ApiController to App_Code folder without wrapping it inside the namespace.. can you then request the ApiController? .. but it should also works when it is wrapped inside a namespace and you have build the project.. the assembly exists in the /bin folder in that case?

    /Bjarne

  • Steve 140 posts 321 karma points
    Mar 24, 2015 @ 21:02
    Steve
    2

    Bjarne,

    Thank you very much - I see the problem now. I put the controller and model in folders (named as such) outside of the App_Code folder. I'm using VWD Express so not building (or able to build) the project (I believe that is a limitation of the Express version).

    I put the model and controller files in the App_Code folder and now all is working.

    Many thanks to you and to kiposeop.

    Best regards,

    Steve

  • Bjarne Fyrstenborg 1285 posts 4039 karma points MVP 8x c-trib
    Mar 24, 2015 @ 21:21
    Bjarne Fyrstenborg
    0

    Great it solved the problem.. but I think you also should be able to add the ApiController in another folder (or even another project in Visual Studio), when it is wrapped inside a namespace.. I also think that the Express version is able the build - if you right click on you website or project in Visual Studio / VWD don't you have a "build" in the context menu?

    You might also have to check that the reference is added to the assembly - if you right click on references below the root node of the website project, you can choose the assembly from the bin foldeer or from another project in your solution.

    /Bjarne

  • Steve 140 posts 321 karma points
    Mar 24, 2015 @ 22:44
    Steve
    0

    Bjarne,

    Yes, you are correct regarding the Build option in the Express version. I haven't built anything since the days of .NET 1.0! Will certainly have a look.

    Now that I've got the very basics WebApi controller working I will experiment with the namespace and file location - and post a reply with results after some further work.

    Thank you very much for the excellent suggestions.

    Best regards, Steve

     

Please Sign in or register to post replies

Write your reply to:

Draft