locked
Simple WebPages "api" RRS feed

  • Question

  • User1100034702 posted

    Hi, just wanted to tell about an experiment I've just made to be able to use WebPages as a restful webapi fw in an easy way. It's a reaction to "do I really need to compile and update the whole application whenever I need to do a small change in my api"? 

    I wrapped the asp.net databinding + managed to connect url's to function names and got quite a useful thing in about 100 lines of code. Please have a look:

    https://github.com/joeriks/WebPagesAdditions

    (or install-package webpagesadditions.boundwebpage )

    Save a little code in projects.cshtml:

    And then you can setup your ajax to talk to for example /api/projects

    @inherits WebPagesAdditions.BoundWebPage
    @functions{
    
        // returns json on the url /api/projects/:id (int)
        public object GET(int id)
        {
            var someObject = new { id = id, data = "whatever" };
            return someObject;
        }
        
        // handles post to /api/projects
        public object POST()
        {
            var project = new Bind(new Project());
            if (ModelState.IsValid) {
                // save object to repository
            }
        }
        
        class Project{
            public int data {get; set;}
        }
    
    }

    Thursday, August 15, 2013 8:20 AM

Answers

  • User1100034702 posted

    Re: Adding AspNet WebApi instead of this webpages style approach

    I'd use a separate project for WebApi code, but it should work in App_Code in WebMatrix file structure aswell.

    But you loose two things I find really cool in WebPages/WebMatrix web sites.

    1) that the file system structure represents the url structure. Never hard to find the code you need to fix.

    2) it compiles the file you add on the fly, without recompiling the whole application and without restarting the app (online users do no loose their ongoing sessions).

    With "bound webpage" you keep these two features.

    That said. Using WebApi is naturally the more robust solution, but this approach could be something to continue to explore if people find it useful. And we should be able to re-use more kinds of functionality from across MVC / WebApi if necessary.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 12:40 AM

All replies

  • User895691971 posted

    Well yeah, not only your API but the code providers API too. When his API changes you need to change your code too. To make sure you are having the best of what you're writing!

    Friday, August 16, 2013 1:22 PM
  • User1100034702 posted

    In this scenario I am the only code provider. This is a very easy way to add server json endpoints for ajax. If you for example use a autocomplete web control and need to write the server side part yourself, or if you like to store data without full page refresh. This is one easy way to do so.

    Tuesday, August 20, 2013 2:46 AM
  • User-908601221 posted

    That is pretty cool.  I wanted to add that you can use the WebAPI framework with webpages/webmatrix and you don't need to compile and redeploy your whole application when changes are made.

    Monday, August 26, 2013 7:09 PM
  • User-821857111 posted

    I wanted to add that you can use the WebAPI framework with webpages/webmatrix and you don't need to compile and redeploy your whole application when changes are made.

    You would have to recompile/redeploy the WebAPI part if you made changes to it.

    Tuesday, August 27, 2013 12:32 AM
  • User895691971 posted

    beetledev

    I wanted to add that you can use the WebAPI framework with webpages/webmatrix and you don't need to compile and redeploy your whole application when changes are made.

    You would have to recompile/redeploy the WebAPI part if you made changes to it.

    I never did that! Do we really need to do this? 

    Tuesday, August 27, 2013 1:18 AM
  • User-908601221 posted

    You would have to recompile/redeploy the WebAPI part if you made changes to it.

    Is it possible to put WebAPI right in the WebMatrix file structure without using a project (web application) structure for the WebAPI parts?

    I followed this article about using WebAPI with WebForms:

    http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with-aspnet-web-forms

    It didn't seem like they did much to get WebAPI to work with WebForms so I tried it using WebPages, but using _AppStart instead of the Global.asax

    I also had to include a few dlls which I got from an existing web application WebAPI project so I couldn't exactly do it from start to finish in only WebMatrix.  I think the ones I copied over Newtonsoft.Json, System.Net.Http.Formatting, System.Web.Http and maybe System.Web.Http.WebHost.

    Perhaps doing it that way won't work 100%, but it seems to work for the basic Get/Put/Post calls.  I never tried Delete yet, but I think it would work too.  

    Tuesday, August 27, 2013 5:03 PM
  • User1100034702 posted

    Re: Adding AspNet WebApi instead of this webpages style approach

    I'd use a separate project for WebApi code, but it should work in App_Code in WebMatrix file structure aswell.

    But you loose two things I find really cool in WebPages/WebMatrix web sites.

    1) that the file system structure represents the url structure. Never hard to find the code you need to fix.

    2) it compiles the file you add on the fly, without recompiling the whole application and without restarting the app (online users do no loose their ongoing sessions).

    With "bound webpage" you keep these two features.

    That said. Using WebApi is naturally the more robust solution, but this approach could be something to continue to explore if people find it useful. And we should be able to re-use more kinds of functionality from across MVC / WebApi if necessary.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 28, 2013 12:40 AM
  • User-821857111 posted

    beetledev

    Mikesdotnetting

    [Brain Freeze]

    You would have to recompile/redeploy the WebAPI part if you made changes to it.

    [/Brain Freeze] 

    Is it possible to put WebAPI right in the WebMatrix file structure without using a project (web application) structure for the WebAPI parts?

    Yes, of course its possible. What I said earlier only applies if you have a separate Web API project, but you don't have to do that. You can add the ApiController to App_Code. If you use Visual Studio (Express) all the correct dlls are added to bin too.

    Wednesday, August 28, 2013 12:45 AM