Copied to clipboard

Flag this post as spam?

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


  • frozen 6 posts 56 karma points
    Feb 24, 2014 @ 16:18
    frozen
    0

    Use Web API with WebForms

    Hi,

    Is it possible to use Web API and WebForms or do I have to use MVC? By the way I'am using Umbraco 6.1.6.

    If it's possible, should I create a folder named Controllers and add my ApiControllers there?

    So if I refer to the example at http://our.umbraco.org/documentation/Reference/WebApi/

    public class ProductsApiController : UmbracoApiController{       
        public IEnumerable<string> GetAllProducts()
        {
            return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
        }
    }
    

    It should be accessible from localhost:some:port/Api/ProductsApi/GetAllProducts right?

    Do I need to setup some routing to make it work?

    Thanks in advance /E

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Feb 24, 2014 @ 16:25
    Stefan Kip
    0

    Is it possible to use Web API and WebForms or do I have to use MVC? By the way I'am using Umbraco 6.1.6

    Yes

    If it's possible, should I create a folder named Controllers and add my ApiControllers there?

    Yes

    It should be accessible from localhost:some:port/Api/ProductsApi/GetAllProducts right?

    No: localhost:some:port/Umbraco/Api/ProductsApi/GetAllProducts
    Unless you include this UrlRewrite rule in your web.config:

    <rule name="Umbraco Web-API">
        <match url="^api/(.+)$" />
        <action type="Rewrite" url="/Umbraco/Api/{R:1}" />
    </rule>
    
  • frozen 6 posts 56 karma points
    Feb 25, 2014 @ 07:37
    frozen
    0

    Thanks, I'll missed the part "Umbraco" in the URL I think, will test again and report back.

    /E

  • frozen 6 posts 56 karma points
    Feb 25, 2014 @ 08:48
    frozen
    0

    Hi again,

    I'am not able to make it work.

    My folder structure.

    • MyProject
      • App_Code
      • ...
      • Config
      • Controllers
        • ProductsApiController.cs
      • ...
      • ...

    ProductsApiController.cs

    using System.Collections.Generic;
    using Umbraco.Web.WebApi;
    
    public class ProductsApiController : UmbracoApiController
    {
      public IEnumerable<string> GetAllProducts()
      {
        return new[] { "Table", "Chair", "Desk", "Computer", "Beer fridge" };
      }
    }
    

    Trying to access the GetAllProducts method by this url: http://localhost:4635/Umbraco/api/ProductsApiController/GetAllProducts

    but it just returns a 404 page.

    /E

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Feb 25, 2014 @ 10:30
    Stefan Kip
    0

    You need to leave out the 'Controller' part, like I mentioned before: /Umbraco/Api/ProductsApi/GetAllProducts

  • frozen 6 posts 56 karma points
    Feb 25, 2014 @ 11:09
    frozen
    0

    Copy paste, is a bad thing :). Also tested with http://localhost:4635/Umbraco/api/ProductsApi/GetAllProducts

    Still no go. Should also mention that it's not the 404 page generated by Umbraco, is a 404 page generated from IIS.

    /E

  • frozen 6 posts 56 karma points
    Feb 25, 2014 @ 11:21
    frozen
    0

    A more detailed description of the 404 page.

    Detailed Error Information:

    • Module: IIS Web Core
    • Notification: MapRequestHandler
    • Handler: StaticFile
    • Error Code 0x80070002

    • Requested URL: http://localhost:4635/Umbraco/Api/ProductsApi/GetAllProducts
    • Physical Path: C:\Users\erab\Documents\My Web Sites\Umbraco CMS\Umbraco\Api\ProductsApi\GetAllProducts
    • Logon Method: Anonymous
    • Logon User: Anonymous
    • Request Tracing Directory: C:\Users\erab\Documents\IISExpress\TraceLogFiles\UMBRACO CMS

    The physical path is point to MyProject\Umbraco\Api... which seems wrong.

    /E

  • frozen 6 posts 56 karma points
    Feb 25, 2014 @ 13:57
    frozen
    100

    Okay, I got it to work. It may be that I misunderstood something, but for others that made the same mistake might be helped by this.

    Instead of creating a folder Controllers under the root, add the controller to the App_Code instead and everything works like a charm.

    Like this:

    • MyProject
      • App_Code
        • ProductsApiController.cs
      • ...
      • Config
      • ...

    I seems that i does not matter if the ProductsApiController.cs is inside a folder as long as it's located in the App_Code folder.

    This also works:

    • MyProject
      • App_Code
        • Controllers
          • ProductsApiController.cs
      • ...
      • Config
      • ...

    Thanks again for the help kipusoep

Please Sign in or register to post replies

Write your reply to:

Draft