Copied to clipboard

Flag this post as spam?

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


  • dan 54 posts 133 karma points
    Sep 03, 2015 @ 03:15
    dan
    0

    Umbraco Controllers All Respond with 404

    I've been struggling with this can can't make any headway why no matter how close I follow the documentation or get help writing a controller, it simply responds with 404.

    I saw this post here, but it doesn't make sense why it works here but not for me.

    https://our.umbraco.org/forum/developers/razor/63485-404-error-when-using-WebApi

    I'm using a namespace, its a brand new Umbraco project, everything compiles just fine, I've searched all over to find out why controllers respond with 404 and hit a wall.

    Here's my controller. Somehow, I have only the first method working; GetMediaId. None of the other methods after it work. I can't find any real difference why they wouldn't work. I'm not even sure why the first method works to begin with when a simpler method doesn't work at all.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using dansandler_com4.Models;
    using Umbraco.Web;
    using Umbraco.Web.WebApi;
     public class MediaApiController : UmbracoApiController
    {
    
    
        [HttpGet]
        public MediaApiModel GetMediaById(int id)
        {
            var media = Umbraco.TypedMedia(id);
            return new MediaApiModel
            {
                MediaId = media.Id,
                MediaUrl = media.Url
                // etc until all properties are set
            };
        }
    
        [HttpGet]
        public MediaApiModel GetMediaTest(int mediaTypeAlias) {
            return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
        }
    
    }
    
    
     public class MediaFileModel
    {
        public int MediaId { get; set; }
    
        public string MediaUrl { get; set; }
    }
    

    What updates are being made to a brand new Umbraco project, with a controller is made to make it work? This seems like it should be a straight forward affair but yet it's 404 only.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 03, 2015 @ 15:00
    Sebastiaan Janssen
    0

    I can't see your usings so please make sure that you're using [System.Web.Http.HttpGet] as opposed to System.Web.Mvc.HttpGet.

    When you do that this URL should work:

    /umbraco/api/MediaApi/GetMediaById/?id=2303

  • dan 54 posts 133 karma points
    Sep 03, 2015 @ 15:07
    dan
    0

    I added my usings to my code above. This is what is inconsistent. The first method in the controller works. Any other one, even if I do a hard coded return doesn't.

    This for example:

     public MediaApiModel GetMediaTest(int mediaTypeAlias) {
            return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
        }
    

    Should return something but comes up as 404.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 03, 2015 @ 15:17
    Sebastiaan Janssen
    0

    So /umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234 returns a 404?

    I'd recommend you put [System.Web.Http.HttpGet] on the GetMediaTest and GetMediaById methods to make sure it's listening for the correct type of HttpGet.

  • dan 54 posts 133 karma points
    Sep 04, 2015 @ 02:40
    dan
    0

    Correct.

    This returns 404

    /umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234
    

    so does this

    /umbraco/api/MediaApi/GetMediaTest/1234
    

    Hard coding the using above each method doesn't change the result.

    [System.Web.Http.HttpGet]
    

    The target framework is 4.5, I'm running umbraco 7.2.8, everything in the web config is default and matches everything I can find online in regards to an impediment for a controller.

    I'm using VS 2012 Ultimate, I tried newer VS express versions and saw the same issue. There has to be something other people are doing that is necessary to make this work.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 04, 2015 @ 08:58
    Sebastiaan Janssen
    0

    The VS version should not matter at all, it just compiles to the same dll.

    Your exact code above works for me when going to /umbraco/api/MediaApi/GetMediaTest/?mediaTypeAlias=1234 so there must be something strange going on in your solution. I am out of suggestions though.. No idea what that could be I'm afraid.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 04, 2015 @ 09:09
    Sebastiaan Janssen
    0

    Here's my test solution, maybe you can find something that's different from yours: https://dl.dropboxusercontent.com/u/3006713/WebApplication30.zip

  • dan 54 posts 133 karma points
    Sep 08, 2015 @ 15:00
    dan
    0

    Hey Sebastiaan,

    Thanks for the sample project. So what I noticed in this zip file is that it appears that when the controllers are created, they are somehow "registered" with the application. I'm saying registered since I don't know how else to explain it. They are identified in some way where they can be accessed later.

    Simply taking your sample, adding a new method to the controller, rebuilding, it comes up 404. The previous methods still work but the new one is unknown. Between your project and mine, I'm not seeing any major differences in the web.config or package.config files that would make this not work for me. I don't know where else to look I'm spot checking around and I'm not seeing anything.

    So I guess where to go from here is when a controller is created, where do its methods get "registered" with the application? How does it know that this controller should be accessible? Is there a way to edit the list of methods the controller has accessible? Like a config file or dll associated with the controller? I really don't know since whatever it is, it seems to be created automatically for everyone else but in my project(s) it is not.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 08, 2015 @ 15:50
    Sebastiaan Janssen
    0

    Umbraco takes care of that.. And it works by writing : UmbracoApiController

    So you're saying that you added a file in the controllers folder in my sample project with a class that inherits from UmbracoApiController and you can not use it?

    Are you sure you don't have any code in your site that alters the routing table?

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 08, 2015 @ 15:51
    Sebastiaan Janssen
    0

    Also, can you list all the content of the file you added to the project here please?

  • dan 54 posts 133 karma points
    Sep 08, 2015 @ 16:42
    dan
    0

    I'm not doing anything to change the default routing.

    So just working in the sample project I added a new controller into the controllers folder.

    using System.Web.Http;
    using Umbraco.Web.WebApi;
    
    namespace WebApplication30.Controllers
    {
        public class MediaGetterController : UmbracoApiController
        {
            [HttpGet]
            public MediaApiModel GetMediaTest2(int id)
            {
                return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
            }
    
            public class MediaApiModel
            {
                public int MediaId;
                public string MediaUrl;
            }
        }
    }
    

    This returns a 404.

    Going back to the original controller "MediaApiController" I changed an argument in the "GetMediaTest" method from "mediaTypeAlias" to "id". This doesn't update when I rebuild and run the site. It only works if I use the old argument in the query string which doesn't even appear in the controller.

    using System.Web.Http;
    using Umbraco.Web.WebApi;
    
    namespace WebApplication30.Controllers
    {
        public class MediaApiController : UmbracoApiController
        {
            [HttpGet]
            public MediaApiModel GetMediaById(int id)
            {
                var media = Umbraco.TypedMedia(id);
                return new MediaApiModel
                {
                    MediaId = media.Id,
                    MediaUrl = media.Url
                };
            }
    
            [HttpGet]
            public MediaApiModel GetMediaTest(int id)
            {
                return new MediaApiModel { MediaId = 000, MediaUrl = "monkey" };
            }
    
            public class MediaApiModel
            {
                public int MediaId;
                public string MediaUrl;
            }
        }
    }
    

    I did notice that the sample project has a dll of the same name as the project. None of my other attempts create that. If I delete that dll from the bin folder, it does not get recreated and the original controller ceases operation.

  • Sebastiaan Janssen 5044 posts 15475 karma points MVP admin hq
    Sep 08, 2015 @ 19:04
    Sebastiaan Janssen
    100

    Okay, it seems like your Visual Studio does not build the project for some reason. If the dll you deleted does not re-appear that means that visual studio is not actually building your project. So first of all, check the build output window in visual studio and make sure it's not showing any errors, if there are errors you need to fix your code.

    If that's not it then there's not much I can help you with there, you need to maybe reset or reinstall visual studio to get that to work again.

  • dan 54 posts 133 karma points
    Sep 12, 2015 @ 22:16
    dan
    0

    Ok, I figured it out. Reinstalled VS, created a new application using the sample project, and its good ad gold now.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft