Click here to Skip to main content
13,298,965 members (40,347 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


6 bookmarked
Posted 21 Feb 2013

Proxy Object Generation for MVC and WebAPI Controllers

, 22 Feb 2013
Rate this:
Please Sign up or sign in to vote.
Using method metadata to create proxy object from T4 templates

This article will explain how to generate proxy objects of MVC and WebAPI controllers, using T4 templates and a wrapper for the controller class which exposes method metadata. This will allow you to make calls on object which will then map directly to a web service, mimicking that of WCF.


I started this project when developing a solution to an assignment involving WebApi where I wanted to recreate a proxy object similar to the one WCF offers.

Using the code

The Wrapper and Attributes

To generate metadata the controller must inherit from its appropriate base class wrapper either ContractedController or ContractedApiController. These contracted controllers expose a method named GetContractInfo() which returns the metadata used by the T4 templates.

Methods that need exposing must be marked with the TransferContractAttribute. This attribute by default defines all methods to use GET, this can be changed by defining the request type in the attribute constructor. For MVC method that return Json data the data type returned must also be specified.

A contracted MVC controller:

public class DemoController :  ContractedController 
  [TransferContract(jsonReturnType: typeof(string))]
  public JsonResult TestMethod()
    return Json("SomeText", JsonRequestBehavior.AllowGet);
  [TransferContract(requestType: RequestType.POST, jsonReturnType: typeof(string))]
  public JsonResult TestMethod(string text)
    return Json("Sent: " + text);

A contracted WebAPI controller: 

public class DemoApiController : ContractedApiController
  public string GetText()
    return "SomeText";
  [TransferContract(requestType: RequestType.POST)]
  public string RepeatText(Message text)
    return "Sent: " + text;
  public class Message
    public string Text { get; set; }

The Generator 

Now that the controllers expose the GetContractInfo() method the metadata can be accessed allowing you to generate a proxy object. I have written a C# and JavaScript T4 template which are bundled with the download.  

Points of Interest  

While creating this demo I had to alter the WebApi controller to take a message type due to the template generating sending data as JSON objects, which was not being picked up by the controller.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionWell done ! Pin
Paul Van Bladel II28-Apr-13 2:27
memberPaul Van Bladel II28-Apr-13 2:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 22 Feb 2013
Article Copyright 2013 by LiamRussell125
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid