Copied to clipboard

Flag this post as spam?

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


  • lucuma 261 posts 563 karma points
    Feb 13, 2014 @ 20:33
    lucuma
    0

    Post to a WebApi controller

    How do you post to a locally declared web api controller? I can't figure it out.

    [HttpPost, HttpGet]
    [AcceptVerbs("POST","GET")]
    public PageViewModel GetPage(string url)
    {
    
    }
    

    None of those attributes work, and only GET will function. Is there some kind of special routing that needs to be setup?

  • Dan Lister 416 posts 1974 karma points c-trib
    Feb 14, 2014 @ 01:04
    Dan Lister
    0

    Have you registered your routes within your global.asax.cs? For example:

    Global.asax

    <%@ Application Inherits="MyApplication.Global" Language="C#" %>

    Global.asax.cs

    namespace MyApplication
    {
        public class Global : UmbracoApplication
        {
            protected override void OnApplicationStarting(object sender, EventArgs e)
            {
                RouteTable.Routes.MapHttpRoute("MyApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
                base.OnApplicationStarting(sender, e);
            }
        }
    }
    
  • lucuma 261 posts 563 karma points
    Feb 14, 2014 @ 01:06
    lucuma
    0

    Do t hey have to be registered only for the POST? The GET works fine.

  • Dan Lister 416 posts 1974 karma points c-trib
    Feb 14, 2014 @ 11:02
    Dan Lister
    0

    I guess if the GET works fine then you shouldn't have to. How are you sending your POST request? Do you receive an error when sending a POST request?

  • lucuma 261 posts 563 karma points
    Feb 14, 2014 @ 18:01
    lucuma
    100

    Nevermind, sometimes these small little things are huge annoyances. There are two HttpPost attribute/decorations. One from system.web.mvc and another from system.web.http. After updating it to the following it works fine or better yet change out the reference and remove the System.Web.Mvc.

        [System.Web.Http.HttpPost]
        [AcceptVerbs("POST","GET")]
        [AllowAnonymous]
        public PageViewModel GetPage(UrlModel url)
        {
            // stuff
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft