Copied to clipboard

Flag this post as spam?

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


  • nickornotto 397 posts 900 karma points
    Aug 10, 2018 @ 11:07
    nickornotto
    0

    I am unable to hit Get method in WebAP

    I have WebAPI2 project in my solution where I;m using umbraco 7 as a startup project.

    I have trouble to hit webapi action. I am getting 500 error in the console and the api is not reached.

    This is my ajax call:

    function getProducts() {
        var response;
        $.getJSON("/api/Product")
            .done(function (data) {
                response = $.map(data,
                    function (item) {
                        return { label: item.Name + ' (' + item.Code + ')' };
                    });
            });
        return response;
    };
    

    The ajax is not hitting. The request skips getJson call and returns undefined response.

    This is my api controller method:

    public class ProductController : ApiController {
    
    // GET api/<controller>
        public IEnumerable<ProductModel> ProductList()
        {
            ProductSearcher searcher = new ProductSearcher();
            return searcher.GetResults();
        }
    }
    

    In the config I have defined:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    

    Postman returns Object reference not set to an instance of an object but it doesn't even hit the ProductList either.

    I have selected to run multiple projects - my app start project and web api project.

    This is the error stacktrace - it looks to me like umbraco does looks for routing while I need to route to non-umbraco api:

    [NullReferenceException: Object reference not set to an instance of an object.]
       Umbraco.Web.Routing.ContentLastChanceFinderByNotFoundHandlers.HandlePageNotFound(PublishedContentRequest docRequest) +152
       Umbraco.Web.Routing.ContentLastChanceFinderByNotFoundHandlers.TryFindContent(PublishedContentRequest docRequest) +10
       Umbraco.Web.Routing.PublishedContentRequestEngine.HandlePublishedContent() +529
       Umbraco.Web.Routing.PublishedContentRequestEngine.FindPublishedContentAndTemplate() +250
       Umbraco.Web.Routing.PublishedContentRequestEngine.PrepareRequest() +107
       Umbraco.Web.UmbracoModule.ProcessRequest(HttpContextBase httpContext) +361
       Umbraco.Web.UmbracoModule.&lt;Init&gt;b__8(Object sender, EventArgs e) +80
       System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
       System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +71
    
  • Nigel Wilson 944 posts 2076 karma points
    Aug 10, 2018 @ 17:59
    Nigel Wilson
    0

    Hi there

    Is it worth updating the umbracoReservedPaths in the appSettings in the web.config with your base url ?

    Cheers

    Nigel

  • nickornotto 397 posts 900 karma points
    Aug 15, 2018 @ 06:49
    nickornotto
    0

    It might have worked as the change to api routing made a difference (see my reply below), I'll check this. Thanks

  • Jon R. Humphrey 164 posts 455 karma points c-trib
    Aug 10, 2018 @ 23:38
    Jon R. Humphrey
    0

    Manila,

    Apologies as I'm not 100% sure this will help but in the code snippets you showed:

    function getProducts() {
        var response; 
        $.getJSON("/api/Product").done( function (data) { 
            response = $.map(data, function (item) { 
                return { label: item.Name + ' (' + item.Code + ')' }; 
            }); 
        }); 
        return response; 
    };
    

    but then in the API controller, you're asking for

    return searcher.GetResults();
    

    While you've not shown the rest of your code, and that's fine - are you 100% sure you're trying to hit the right endpoint?

    Happy to chat, look forward to your response!

    Kind regards, Jon

  • nickornotto 397 posts 900 karma points
    Aug 15, 2018 @ 06:47
    nickornotto
    100

    Hi, The content of GetResults() is irrelevant in this moment as the containing method does not even get hit.

    I have solved this by changing the api routing, seee the solution here: https://stackoverflow.com/questions/51785141/i-am-unable-to-hit-get-method-in-webapi-with-umbraco-project

    Although I'm struggling to get the response on the ks site in my project. It displays well in the webapi template though so the api method is working, I just have no idea while the response is not passed through to the calling project, see the other thread: https://stackoverflow.com/questions/51792538/ajax-call-to-webapi-does-not-hit-the-ajax-response-and-not-returning-results

  • suman 30 posts 101 karma points
    Aug 14, 2018 @ 12:33
    suman
    0

    Hi Manila,

    Can you try this,

    Your controller should inherit from "UmbracoApiController". And your $.getJSON("umbraco/api/Product")

  • nickornotto 397 posts 900 karma points
    Aug 15, 2018 @ 06:48
    nickornotto
    0

    Thanks suman but this is not a solution, I don't use umbraco api and I won't in this project so there is no reason to load the whole bunch of umbraco dependencies just to perform a get on the other project.

Please Sign in or register to post replies

Write your reply to:

Draft