Click here to Skip to main content
15,887,027 members
Articles / Web Development / HTML
Tip/Trick

Build Platform Independent Applications Using Bridge.NET

Rate me:
Please Sign up or sign in to vote.
4.82/5 (18 votes)
2 Apr 2015CPOL4 min read 32.9K   342   32   10
How to use Bridge.NET to convert your C# code into a JavaScript application featuring Bootstrap and jQuery.

Introduction

Bridge.NET is an open source product that lets you compile your C# code into JavaScript. Furthermore, it supports popular frameworks such as HTML5, jQuery and Bootstrap right out of the box.

In this article, I will demonstrate how Bridge.NET works by building an application that dynamically creates web forms based on some template data. What you end up with is a beautiful JavaScript application automatically written by the Bridge.NET transpiler that can:

  • Fetch form template data in JSON format by calling a .NET HTTP Handler.
  • Create the bootstrap form based on that data. In this particular case, the form is a simple questionnaire showcasing text and radio input controls.
  • Post filled form data back to the server.

Figure 1 illustrates what the resulting questionnaire form looks like in the browser:

Figure 1: The questionnaire form

Setup

Figure 2 shows the structure of the Visual Studio solution. Two projects are involved:

  1. A Class Library project (FormBuilder) that acts as the Bridge.NET translator. This is the target project to install the Bridge.Bootstrap NuGet package into.
    In the Package Manager Console, enter the command:

    Install-Package Bridge.Bootstrap

    In this project, you add your C# source files (.cs) that get translated to JavaScript every time the project is (re)build. The generated .js files are output in the project's \Bridge\output folder, by default. You can configure how Bridge.NET operates by modifying the bridge.json configuration file as well as the clean.bat and deploy.bat files found inside the \Bridge\build folder. For the purposes of this article, here is the desired Bridge.NET configuration:

    • bridge.json:
      {
      		    output: "Bridge\\output",
      		    beforeBuild: "Bridge\\build\\clean.bat",
      		    afterBuild: "Bridge\\build\\deploy.bat"
      		}
    • deploy.bat:
      SET _=%~dp0
      		SET dest="%_%..\..\..\Questionnaire\resources\js"
      		IF EXIST %dest% xcopy "%_%..\output" %dest% /Y
  2. A Web Application project (Questionnaire). This is the project that will make use of the generated JavaScript files. It can be either a WebForms or MVC project and should be set to the default project of the solution. The only thing you need to do here is add a \resources\js folder to the project. Note that this is the path used in the deploy.bat file above. In effect, every time you successfully (re)build the Class Library project, the generated JavaScript files will be automatically copied to the Web Application project.

    Tip: You need to toggle the Show All Files option on in Solution Explorer in order to view the .js files since they are not included in the project, by default.

You can read all about setting up Bridge.NET in this excellent Getting Started article. There is also the Global Configuration article that explains configuration options in detail.

Using the Code

FormTemplate.ashx

Let us start with the HTTP Handler that serves us with the form template data. The FormTemplate.ashx file resides in the Web Application project and returns JSON data in a very simple format:

C#
private string[] Template
{
    get
    {
        return new string[]
        {
            "0|name|Full Name|false",
            "0|email|Email|true",
            "1|q1|Favorite Browser|IE,Firefox,Chrome,Safari",
            "1|q2|Favorite Language|C#,JavaScript,Both"
        };
    }
}

The data describing the form is simply returned as an Array of string with characters "|" and "," used to separate fields. This is intentional to keep things as simple as possible in this example. You will probably want to model the form structure using Classes and have the HTTP Handler serialize and return those objects instead.

The Json.NET library is used to perform JSON serialization. This is not directly related to Bridge.NET; it is a matter of personal preference -you can use your favorite JSON library, instead.

FormFactory.cs

This is where you define the FormFactory Class that will be translated to JavaScript. Use the Bridge.NET [Ready] attribute to decorate the method you want to get called first as soon as the browser is done parsing the hosting web page:

C#
[Ready]
public static void Main()
{
    // fetch form template from http handler and start building form
    jQuery.Ajax(
        new AjaxOptions()
        {
            Url = FormFactory.FORM_TEMPLATE_DATA,
            Cache = false,
            Success = delegate(object data, string str, jqXHR jqXHR)
            {
                FormFactory.CreateForm(jQuery.Select(FORM_CONTAINER), (Array)data);
            }
        }
    );
}

The Bridge.jQuery2.jQuery.Ajax method should be straightforward enough. Upon success, the CreateForm method is called to start building the form.

C#
public static void CreateForm(jQuery container, Array template)
{
    foreach (string t in template)
    {
        container.Append(FormFactory.CreateFormField(t));
    }
}

Note how HTML5 elements are modeled into Classes. For example, let us take a look at how a Text Input field is constructed using the InputElement class:

C#
public static jQuery CreateTextInput(string id, string label, bool required = false)
{
    return new jQuery("<div>")
        .AddClass("form-group")
        .Append(new LabelElement
            {
                ClassName = "control-label col-sm-2",
                HtmlFor = id,
                InnerHTML = label + ":"
            })
        .Append(new jQuery("<div>")
            .AddClass("col-sm-10")
            .Append(new InputElement
            {
                Type = InputType.Text,
                Id = id,
                Name = id,
                Required = required,
                ClassName = "form-control"
            })
        );
}

Index.html

This is the hosting web page. Please note that this can be a simple HTML page since everything runs in client side JavaScript code, ultimately. The web page:

  • References all necessary JavaScript files. Bridge.NET produces both a debugger friendly and a minified version of your code. Make sure you add a reference to bridge.js or bridge.min.js, too.
  • Provides the container HTML element (form1) where the dynamically created form is added into.
HTML
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Bridge.NET</title>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="//netdna.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

    <script src="resources/js/bridge.js"></script>
    <script src="resources/js/formBuilder.js"></script>
</head>
<body style="padding:20px">
    <div class="container">
        <div class="panel panel-default">
            <div class="panel-heading">
            <h1 class="panel-title">Bridge.NET Questionnaire</h1></div>
            <div class="panel-body">
                <form id="form1" class="form-horizontal" role="form">
                </form>
            </div>
        </div>
    </div>
</body>
</html>

Points of Interest

It is interesting to note the way jQuery and Bootstrap are supported by Bridge.NET in addition to HTML5 and the C# language, of course.

I also recommend you take a look at the generated JavaScript code. Below is an extract of the FormFactory class translation:

JavaScript
Bridge.Class.define('FormBuilder.FormFactory', {
    statics: {
        $config:  {
            fields: {
                FORM_TEMPLATE_DATA: "FormTemplate.ashx",
                FORM_CONTAINER: "#form1",
                SUBMIT_URL: "/Submit.aspx"
            },
            init: function () {
                Bridge.ready(this.main);
            }
        },
        main: function () {
            // fetch form template from http handler and start building form
            $.ajax({ url: FormBuilder.FormFactory.FORM_TEMPLATE_DATA, cache: false, 
                     success: function (data, str, jqXHR) {
                       FormBuilder.FormFactory.createForm($(FormBuilder.FormFactory.FORM_CONTAINER), 
                       Bridge.cast(data, Array));
            } });
        },

You will be surprised at the number of C# features supported by Bridge.NET. It even supports the async and await keywords for asynchronous C# programming!

Last but not least, it is important that Bridge.NET is open source licensed under Apache 2.0. If you are really interested in getting involved, pull requests are accepted on GitHub.

Happy coding with Bridge.NET. :)

License

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


Written By
Software Developer (Senior)
Greece Greece
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun10-May-15 18:18
Humayun Kabir Mamun10-May-15 18:18 
QuestionWindows Forms Pin
Cecil Manley7-Apr-15 0:07
Cecil Manley7-Apr-15 0:07 
AnswerRe: Windows Forms Pin
dimpant7-Apr-15 9:32
dimpant7-Apr-15 9:32 
GeneralRe: Windows Forms Pin
Cecil Manley7-Apr-15 21:37
Cecil Manley7-Apr-15 21:37 
GeneralRe: Windows Forms Pin
dimpant7-Apr-15 23:38
dimpant7-Apr-15 23:38 
GeneralRe: Windows Forms Pin
Cecil Manley8-Apr-15 1:52
Cecil Manley8-Apr-15 1:52 
GeneralRe: Windows Forms Pin
MichalisEU19-May-15 7:23
MichalisEU19-May-15 7:23 
QuestionHave you consider to post this as a tip? Pin
Nelek2-Apr-15 9:02
protectorNelek2-Apr-15 9:02 
AnswerRe: Have you consider to post this as a tip? Pin
dimpant6-Apr-15 9:37
dimpant6-Apr-15 9:37 
GeneralRe: Have you consider to post this as a tip? Pin
Nelek7-Apr-15 7:39
protectorNelek7-Apr-15 7:39 

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.