Skip to content
Home » HBD.Mef.Mvc Enhancement For WebApi

HBD.Mef.Mvc Enhancement For WebApi

Like the previous post, I have introduced a Workspace for Web Mvc and along with the post, AzureNotes Module was provided as an example.

Now in this post, I would like to update you the new version 1.0.5 of HBD.Mef.Mvc. From this version, it will support the WebApi technology that allows developing a similar Workspace for Web API.

The HBD.Api.Shell

Similarly to HBD.Mvc.Shell, the HBD.Api.Shell is the Workspace allowing to develop a WebApi module in the different project and deploy to the Workspace lately.

Develop a WebApi module is simpler than develop a module for Wb Mvc because WebApi doesn’t have any resources (views, CSS files, and JavaScript files) other than controllers. Hence, deployment a module into the Shell is just copying the binaries and config files only.

To simplify the life, I have provided a general routing for all Modules including the Workspace as below.

public static class WebApiConfig 
{ 
    private const string ShellName = "Shell"; 

    public static void Register(HttpConfiguration config) 
    { 
         // Web API configuration and services 
         // Web API routes 
         config.MapHttpAttributeRoutes(); 

         config.Routes.MapHttpRoute(
             "DefaultApi",
             "api/{area}/{controller}/{id}", 
             new {area = ShellName, controller = "About", id = RouteParameter.Optional} );
         }
}

This route is applying for all modules and all controllers. Hence, the pathway to access to a controller is api/{area}/{controller}{id} which {area} is Module’s name, {controller} is the controller name and {id} is optional.

The module registration also simplified instead of providing the Module Name and custom routing. Promptly with API module requires the name only. Below is sample code to register an API module.

public class AzureNoteApiRegister : MefApiRegistration 
{ 
    public override string AreaName => "AzureNote"; 
}

The HBD.Mef.Mvc will base on the registered named and select the appropriate controller for the request. If there is no controller found for an appropriate request the HttpResponseException with 404 status code will be thrown.

Mef Controller Resolver Service

Please note that this registration is for Api modules only. There is nothing changed on the Mvc module registration that had been introduced on the previous post.

The AzureNoteApi

I would like to show you a similar module for HBD.Api.Shell called AzureNoteApi; this module is just exposing all actions of AzureNote in the previous post as WebApi. Below is the details information the Apis. You can use Postman to try out with my live demo.

Live Demo

Sorry the Azure subscription had been expired

The HBD.Api.Shell is also published on to Azure you might have a try by using Postman with the following API.

  • HBD.Api.Shell API
public class AboutController : ApiController 
{
    /// Get http://hbdapishell.azurewebsites.net/api/Shell/About 
    [HttpGet] public AboutInfo Get(); 
}
  • AzureNote API
public class AboutController : ApiController 
{ 
    /// Get http://hbdapishell.azurewebsites.net/api/AzureNote/About 
    [HttpGet] 
    public string Get(); 
}

public class NotesController : ApiController 
{ 
    /// Get http://hbdapishell.azurewebsites.net/api/AzureNote/Notes 
    [HttpGet] 
    public Task<IList<Note>> Get(); 

    /// Get http://hbdapishell.azurewebsites.net/api/AzureNote/Notes/084cfe33-f487-4ae9-8b28-c2e2566e1e48 
    [HttpGet] 
    public async Task<Note> Get(string id);

    /// Post http://hbdapishell.azurewebsites.net/api/AzureNote/Notes 
    /// Create a new Note which body is Json of Note object. 
    [HttpPost]
    public Task<IHttpActionResult> Post([FromBody] Note note);
   
    /// Put http://hbdapishell.azurewebsites.net/api/AzureNote/Notes/084cfe33-f487-4ae9-8b28-c2e2566e1e48 
    /// Update an existing Note which body is Json of Note object. 
    [HttpPut] 
    public Task<IHttpActionResult> Put(string id, [FromBody] Note note); 

    /// Delete http://hbdapishell.azurewebsites.net/api/AzureNote/Notes/084cfe33-f487-4ae9-8b28-c2e2566e1e48 
    [HttpDelete] 
    public async Task<IHttpActionResult> Delete(string id); 
}

Source Code

The latest source codes had been uploaded to Github you can download here.

2 thoughts on “HBD.Mef.Mvc Enhancement For WebApi”

  1. I was excited to discover this website. I want to to thank you for your time for this wonderful read!! I definitely liked every part of it and i also have you book-marked to look at new things on your website.

  2. Im very pleased to find this site. I need to to thank you for ones time for this particularly fantastic read!! I definitely really liked every part of it and I have you bookmarked to see new information on your site.

Leave a Reply

Your email address will not be published.