Copied to clipboard

Flag this post as spam?

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


  • Charles Zipp 20 posts 81 karma points
    Jun 08, 2013 @ 19:12
    Charles Zipp
    0

    Umbraco 6.1.1, Posting form to UmbracoApiController throwing exception

    Hi,

    I had previously integrated web api using the old method of using IApplicationEventHandler
    method to register the api route. I am trying to implement the native WebApi integration with UmbracoApiController.

    I have a contour form whose workflow posts to my web api. However, i am getting a WebException "The remote server returned an error: (404) Not Found" This is being thrown out of MvcRenderContourForm.cshtml. I have also tried just doing a simple get to see if i can get it to hit a break point. I cant get any breakpoints to be hit. Even tried an empty constructor incase this was something to do with ninject.

    I think i am missing a step or not understanding the url format. Any help would be appriciated. I am going to have to revert back to my customized solution (using IApplicationEventHandler) until i can figure this out. Was really hoping to drop the custom by upgrading to 6.1

    Post to Url settings (using url http://localhost:25910/Umbraco/BhpiPublicApi/ContactRequest)

       [PluginController("BhpiPublicApi")]
        public class ContactRequestApiController : UmbracoApiController
        {
            protected readonly IContactRequestSvc _contactRequestSvc;
    
            public ContactRequestApiController()
            {
            }
    
            [Inject]
            public ContactRequestApiController(IContactRequestSvc contactRequestSvc)
            {
                _contactRequestSvc = contactRequestSvc;
            }
    
            [HttpPost]
            public HttpResponseMessage PostContactRequest([FromBody]NotifyContactRequest model)
            {
                _contactRequestSvc.PostContactRequest(model);
                var response = Request.CreateResponse<NotifyContactRequest>(HttpStatusCode.Created, model);
    
                string uri = Url.Link("DefaultApi"new { id = model.NotifyContactRequestId });
                response.Headers.Location = new Uri(uri);
    
                return response;
            }
    
            [HttpGet]
            public NotifyContactRequest GetContactRequest(int notifyContactRequestId)
            {
                return _contactRequestSvc.GetContactRequest(notifyContactRequestId);
            }
        }

  • Charles Zipp 20 posts 81 karma points
    Jun 10, 2013 @ 04:53
    Charles Zipp
    100

    Incase anyone else comes across this... I was able to figure this out. Basically i had the wrong url in the workflow settings.

    I used the UrlHelper extension methods mentioned here to figure out what i should be putting in the workflow settings. I put this code in the Form.cshtml under the umbraco\plugins\umbracoContour\Views folder with a breakpoint to see what the 'test' variable would be.

    @using Umbraco.Web;
    @using Bhpi.Web.Api;
    @{
        string test = Url.GetUmbracoApiService<ContactRequestApiController>("PostContactRequest");
    }

    That gave me the appropriate URL to use in the settings. After I used that url, all started working fine!

    One take away from this though. I think it helps if you explicitly include your action names in your url. In very simple Web Api, the 'PostContactRequest' action would be inferred by doing a post to Api/ContactRequest provided the controller is named ContactRequestApiController. Could possibly have to do with the way the routes are configured in Umbraco. It may be handy to have the route definitions included in the developer api docs for web api.

Please Sign in or register to post replies

Write your reply to:

Draft