Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Develop WebAPI using mono

Is there any guide or documentation to build a REST API using Mono console or MonoDevelop. I tried to create MVC application in MonoDevelop however couldn't find App_Start/WebApiConfig.cs or relevant files by which I can define routes and other settings which I usually do in Visual studio based application.

like image 990
Avi Kenjale Avatar asked Feb 07 '26 03:02

Avi Kenjale


2 Answers

Short answer is there is no template in MD for this, however it's very easy to get going:

  1. Create a new ASP.NET Project (the bottom project in the MD ASP.NET project templates).
  2. Use NuGet to add ASP.NET Web API 2.2.
  3. Add an App_Start folder manually, and a WebApiConfig.cs with your routes etc.

Something like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace MyWebApi
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // TODO: Add any additional configuration code.

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}
  1. Add WebApiConfig configuration into the Global.asax.cs

For example:

protected void Application_Start (Object sender, EventArgs e)
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}
  1. Then start adding your models and controllers etc. as usual.

Hope this help, M.

like image 83
muszeo Avatar answered Feb 12 '26 05:02

muszeo


I followed the instructions given in @muszeo's answer and it worked.

I've created a sample project at https://github.com/sashoalm/HelloWebApi. It defines a single controller called HelloWebApiController, which creates an endpoint at http://localhost:8080/api/HelloWebApi that returns a string with "hello, world".

I've tested it on Linux with MonoDevelop 5.10.

You can clone it using git clone https://github.com/sashoalm/HelloWebApi.git

like image 36
sashoalm Avatar answered Feb 12 '26 03:02

sashoalm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!