MVC4 – Internet Application Template

This is my third post on Exploring MVC 4 Templates. & i will discuss“Internet Application Template”

In Visual Studio 2010 . Create a new ASP.NET MVC4 Project.  Select “Internet Application” from given list of Templates & click OK.

MVC4_InternetApplication

This should create a project for you with following defaults.

MVC4_InternetApplication_Defaults

We can see a HomeController.cs file added under Controllers with a corresponding Home View.

Hit F5 & you should be able to see the application running.

Advertisements

Using MVC4 WebAPI for CRUD operations on MongoDB

Objective :

In this post i will quickly explain how can we write MVC 4 WebAPI to perform complete CRUD operations on any datasource ( In this sample i have used MongoDB).

Backgroud :

It is suggested that you review this post that will enable you to quickly understand MongoDB  connectivity Via .net

Web API is Microsoft’s promise of completely embracing HTTP protocol. For its reason of being the most simple/basic mechanism of exposing & consuming services, it primarily targets browsers, mobile devices & reaching wide audience.

Ingredients :

MongoDB, Visual Studio 2010, MVC4, Fiddler2

Recipe :

Creating WebAPI Project

Open Visual Studio 2010 > Add New Project > Web > MVC 4 Web Application

It will show a dialog box enlisting MVC4 Templates. > Select Web API & Click OK.

01

Creating Entity:

Today i am interested to work with a Vehicle entity. Therefore add a class with name “Vehicle” in Models Folder & add some attributes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson.Serialization.Attributes;
namespace VehicleWebAPI.Models
 {
 public class Vehicle
 {
 [BsonId]
 public string Id { get; set; }
 public string Make { get; set; }
 public string Model { get; set; }
 public Int32 Year { get; set; }
 public float Price { get; set; }
 }
 }

Notice how we have decorated id attribute with BSONId, indicating that this attribute would contain MongoDB document id property.

Creating Entity Repository :

Next we want to use Repository Pattern, therefore add an interface in Models folder “IVehicleRepository” with following code

</code>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using MongoDB;

using MongoDB.Driver;

using MongoDB.Driver.Builders;

namespace VehicleWebAPI.Models

{

public interface IVehicleRepository

{

IEnumerable<Vehicle> GetAllVehicles();

IEnumerable<Vehicle> GetAllVehiclesByMake(string vehicleMake);

IEnumerable<Vehicle> GetAllVehiclesByMake(string vehicleMake,int year);

bool Update(string objectId,Vehicle p);

Vehicle Add(Vehicle p);

bool Delete(string objectId);

}

}

Implementing Repository

Now we will incorporate Implementation detail of our Repository in Models. Add a class naming “VehicleRepository” .

</code>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using System.Configuration;

using MongoDB;

using MongoDB.Driver;

using MongoDB.Driver.Builders;

using MongoDB.Bson;

namespace VehicleWebAPI.Models

{

public class VehicleRepository : IVehicleRepository

{

MongoServer mongoServer = null;

MongoDatabase mDB = null;

MongoCollection mCollection = null;

#region TestData

Vehicle[] _TestData = new Vehicle[]

{

new Vehicle { Id = Guid.NewGuid().ToString(),  Make = "Toyota", Model ="Hilux", Year = 2001 , Price =2400000 },

new Vehicle { Id = Guid.NewGuid().ToString(),  Make = "Honda", Model ="Accord", Year = 2001, Price =3200000},

new Vehicle { Id = Guid.NewGuid().ToString(),  Make = "Toyota", Model ="Hilux", Year = 2002 , Price =2700000 },

new Vehicle { Id = Guid.NewGuid().ToString(),  Make = "Honda", Model ="Accord", Year = 2002, Price =3500000},

new Vehicle { Id = Guid.NewGuid().ToString(),  Make = "Toyota", Model ="Hilux", Year = 2003 , Price =2900000 },

new Vehicle { Id = Guid.NewGuid().ToString(),  Make = "Honda", Model ="Accord", Year = 2003, Price =3800000},

};

#endregion

private List<Vehicle> vehicles = new List<Vehicle>();

public VehicleRepository()

{

#region INIT_MongoCredentials

try

{

mongoServer = MongoServer.Create();

mongoServer.Connect();

mDB = mongoServer.GetDatabase("dbVehicleStore"); /// Move this info to Configuration

mCollection = mDB.GetCollection<Vehicle>("VehicleRecords");

}

catch (Exception ex)

{

throw;

}

#endregion

}

public IEnumerable<Vehicle> GetAllVehicles()

{

#region GetFromDB

if (Convert.ToInt32(mCollection.Count()) > 0)

{

vehicles.Clear();

var VehicleCollection = mCollection.FindAs(typeof(Vehicle), Query.NE("Make", "null"));

if (VehicleCollection.Count() > 0)

{

foreach (Vehicle objVehicle in VehicleCollection)

{

vehicles.Add(objVehicle);

}

}

}

else

{

#region Add some test data since our DB is empty

mCollection.RemoveAll();

foreach (Vehicle iVehicle in _TestData)

{

vehicles.Add(iVehicle);

//save data to collection

Add(iVehicle);

}

#endregion

}

#endregion

var results = vehicles.AsQueryable();

return results;

}

public IEnumerable<Vehicle> GetAllVehiclesByMake(string vehicleMake)

{

if (string.IsNullOrEmpty(vehicleMake))

{

vehicleMake = (string.IsNullOrEmpty(vehicleMake)) ? ConfigurationManager.AppSettings["VehicleDefaultMake"] : vehicleMake;

}

vehicles.Clear();

var VehicleCollection = mCollection.FindAs(typeof(Vehicle), Query.EQ("Make", vehicleMake));

if (VehicleCollection.Count() > 0)

{

foreach (Vehicle objVehicle in VehicleCollection)

{

vehicles.Add(objVehicle);

}

}

return vehicles.AsQueryable();

}

public IEnumerable<Vehicle> GetAllVehiclesByMake(string make,int year)

{

if (string.IsNullOrEmpty(make))

{

make = (string.IsNullOrEmpty(make)) ? ConfigurationManager.AppSettings["VehicleDefaultMake"] : make;

}

vehicles.Clear();

var iQuery = Query.And(Query.EQ("Make", make),Query.EQ("Year", year));

var VehicleCollection = mCollection.FindAs(typeof(Vehicle), iQuery );

if (Convert.ToInt32(VehicleCollection.Count()) > 0)

{

foreach (Vehicle objVehicle in VehicleCollection)

{

vehicles.Add(objVehicle);

}

}

return vehicles.AsQueryable();

}

public bool Update(string objectId , Vehicle vehicle )

{

UpdateBuilder updateBuilder = MongoDB.Driver.Builders.Update.Set("Make", vehicle.Make)

.Set("Model", vehicle.Model)

.Set("Year", vehicle.Year)

.Set("Price", vehicle.Price);

mCollection.Update(Query.EQ("_id", objectId), updateBuilder);

return true;

}

public Vehicle Add(Vehicle vehicle)

{

if (string.IsNullOrEmpty(vehicle.Id))

{

vehicle.Id = Guid.NewGuid().ToString();

}

mCollection.Save(vehicle);

return vehicle;

}

public bool Delete(string objectId)

{

mCollection.Remove(Query.EQ("_id", objectId));

return true;

}

}

}

<code>

Notice how testData is used to fill some default data.

Good times are coming. We have our repository ready.

Creating Controller:

We need to add a controller file to handle HTTP requests.

Go to Controller folder > Delete ValuesContainer Class > Add a new controller > Use “Empty API Container” Template.

Notice that this container class is derived from APIController.

Now we need following definition.

</code>

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using VehicleWebAPI.Models;

namespace VehicleWebAPI.Controllers

{

public class VehicleController : ApiController

{

private static readonly IVehicleRepository _VehicleRepository = new VehicleRepository();

public VehicleController()

{}

// GET api/Vehicles

public IEnumerable<Vehicle> GetAllVehicles()

{

return _VehicleRepository.GetAllVehicles();

}

public IEnumerable<Vehicle> GetAllVehiclesByMake(string make)

{

return _VehicleRepository.GetAllVehiclesByMake(make);

}

public IEnumerable<Vehicle> GetAllDinasaursByFamilyAndYear(string make,int  year)

{

return _VehicleRepository.GetAllVehiclesByMake(make,year);

}

public Vehicle Post(Vehicle p)

{

_VehicleRepository.Add(p);

return p;

}

public HttpResponseMessage Put(string id, Vehicle vehicle)

{

if (!_VehicleRepository.Update(id,vehicle))

{

return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Unable to Update the Vehicle for the Given ID");

}

else

{

return Request.CreateResponse(HttpStatusCode.OK);

}

}

public HttpResponseMessage DeleteVehicle(string id)

{

_VehicleRepository.Delete(id);

return new HttpResponseMessage(HttpStatusCode.NoContent);

}

}

}

We have now our Web API Ready.It contains 6 methods that are accessible via Http requests. Each of these method in Controller is mapped to a URI

// GET : api/Vehicle

GetAllVehicles() : Returns a list of all Vehicle Objects that exist in data source

// GET :  api/Vehicle?make=

GetAllVehiclesByMake(string make) : Returns a list of all Vehicle Objects of a Certain Make ( eg: Honda / Toyota)

// GET : api/Vehicle?make=Honda&&year=2003

GetAllDinasaursByFamilyAndYear(string make,int  year) : Returns a list of all Vehicle Objects of a Certain Make & year.

//POST :  api/Vehicle

Post(Vehicle p) : Returns a single Vehicle Object that has been just added to datasource

//PUT :  api/Vehicle?id=

Put(string id, Vehicle vehicle) : Returns a HTTPResponseMessage

// DELETE : api/Vehicle?id=

DeleteVehicle(string id) : Returns a HTTPResponseMessage

Notice how i have named GetAllDinasaursByFamilyAndYear() method which will fetch vehicles of certain make & year. This is to hint that WebAPI has nothing to do with Names , It will search for method starting with GET &  appropriate parameters.

Testing your application

Via Fiddler

In this section i will demonstrate how to test your web Api with Fiddler

Run your Web API Project from Visual Studio.

Launch Fiddler. Go to Composer Tab & provide the URL of your web API.

GET Requests

Start testing with GET Request . Provide your WEB API URL ( eg : http://localhost:8085/api/vehicle)

Click on Execute Button.

02Fidd

Now you should be able to see a response result in left most result window. Double clicking the JSON result would open the Inspector tab & the JSON result.

Now if you just make a small change in the request header in Composer Tab

Content-type : application/xml

Now Hit Execute again, You should be able to see a XML result in the results window. Double clicking it would show the same data in XML format.

Now provide a filter in URL , under composer tab.

http://localhost:8085/api/vehicle?make=Honda

This would result a JSON/XML result listing all Vehicles from Data source from Honda.( Result format depends on content type we use.

03Fidd

POST Request

We now want to test POST Request therefore we would select POST request & provide default URL http://localhost:8085/api/vehicle

Also we need to provide new record information in Request Body pane

{“Make”:”Honda”,”Model”:”Accord”,”Year”:2013,”Price”:4500000.0}

Hit Execute to Insert this data in our data source.

04Fidd

You can now use GET requests to see if current record has been inserted into the system

PUT Request

We now want to test PUT Request therefore we would select PUT request & provide default URL http://localhost:8085/api/vehicle?id=xxxxxxxxxxx

We need to provide VehicleID & a vehicle object that would be used to update existing values.

Therefore we would provide Id in URI & new record information in Request Body pane

{“Make”:”Honda”,”Model”:”Accord”,”Year”:2015,”Price”:8500000.0}

Hit Execute to Update this data in our data source.

You can now use GET requests to see if previous record was Updated correctly into the system.

DELETE Request

We now want to test DELETE Request therefore we would select DELETE request & provide default URL http://localhost:8085/api/vehicle?id=xxxxxxxxxxx

We need to provide VehicleObject ID in URI.

Hit Execute to Delete record.

You can now use GET requests to validate your DELETE action.

*It is very Important to know that the filter values we provide are case sensitive.

I hope this post has provided you a quick insight on Implementing CRUD in MVC4 – WebAPI.

       

Using Twitter API via .NET

Objective :

In this post i will quickly explain how can we interact with Twitter API using .Net code.

Ingredients :

LinqToTwitter , Visual Studio 2010

Recipe :

You may find several popular Dlls on internet that communicate with Twitter. I have used LinqToTwitter & have found it easy to understand & implement.

You need to have “twitterConsumerKey” & “twitterConsumerSecret” that you will use from within your application. Get your applications twitter credentials from https://dev.twitter.com/apps

Our application URL will be like “http://localhost:PortNumber” but once you are on Twitter Applications settings Form : It does not validate “http://localhost:PortNumber” , therefore an alternate is to use “http://127.0.0.1:PortNumber” as our website placeholder. it will serve our purpose. Twitter uses this fully qualified URL in the source attribution for tweets created by your application and it be shown in user-facing authorization screens.

.NET code

Create a simple Asp.net web application in visual studio 2010.

Web.Config Changes

Add following in your web.config appSettings

<add key="twitterConsumerKey" value="twitterConsumerKey"/><add key="twitterConsumerSecret" value="twitterConsumerSecret"/>

Namespace

Add following namespace in your code file.

using LinqToTwitter;

Code

I used following code to explore Search feature & User Context.

private WebAuthorizer auth; private TwitterContext twitterCtx;

protected void Page_Load(object sender, EventArgs e)

{

IOAuthCredentials credentials = new SessionStateCredentials();

if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)

{

credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];

credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];

}

auth = new WebAuthorizer

{

Credentials = credentials,

PerformRedirect = authUrl => Response.Redirect(authUrl)

};

if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)

{

auth.CompleteAuthorization(Request.Url);

}

if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||

string.IsNullOrWhiteSpace(credentials.ConsumerSecret))

{

//Do nothing

}

else if (auth.IsAuthorized)

{

//Do nothing

}

else

{

auth.BeginAuthorization(Request.Url);

if (!auth.IsAuthorized)

{

Uri specialUri = new Uri(Request.Url.ToString());

auth.BeginAuthorization(specialUri);

}

}

if (auth.IsAuthorized)

{

twitterCtx = new TwitterContext(auth);

#region Search Text

var searchResult =

(from srch in twitterCtx.Search

where srch.Type == SearchType.Search &&

srch.Query == "Brian Adams"

select srch)

.SingleOrDefault();

gvTweetView.DataSource = searchResult.Statuses;

gvTweetView.DataBind();

#endregion

#region UserInfo

var users =

(from user in twitterCtx.User

where user.Type == UserType.Lookup &&

user.ScreenName == "prosarfraz"

select user);

string strHtml = string.Empty;

foreach (User objUser in users)

{

strHtml += "";

strHtml += " Screen Name ";

strHtml += "" + objUser.ScreenName + "";

strHtml += " Favorites Count ";

strHtml += "" + objUser.FavoritesCount + "";

strHtml += " Followers ";

strHtml += "" + objUser.FollowersCount + "";

strHtml += " Description ";

strHtml += "" + objUser.Description + "";

strHtml += "";

}

ulsrc.InnerHtml = "" + strHtml + "";
<p style="padding-left: 60px;">#endregion}}

Executing this code will redirect the web page user to twitter authentication page. One you authorize from your twitter account it will redirect you back to the application page to process #SearchResult & #UserInfo regions.

I hope this has provided you a quick insight on how to connect to Twitter Api using C# code.

Using MongoDB via .NET

Objective :

In this post i will explain how quickly we can interact with NoSQL database using .Net.

Abstract:

NoSQL databases have gained popularity for their simpler design, convenience in usage & finer control in contrast with SQL database. They are being used for simple content management requirements like data retrieval & append, that demands key-value data structure. The whole idea is to use simple database for simpler requirements. I have used Mongodb  & Visual Studio 2010 to achieve this goal.

Setting up MongoDB :

For many developers this is going to be their first encounter with NoSQL database. Therefore i will simply suggest readers to navigate MongoDB.

Read pre-requisite & then download MongoDB database. For our demonstration purpose I have used “Mongodb-win32-i386-2.2.3”

Configure Mongodb.  Make sure you make a “Data\db” directory at drive Root where your MongoDB (Mongodb-win32-i386-2.2.3) folder . MongoDB System will store all its data under “Data\db“.

For convenience i renamed this(Mongodb-win32-i386-2.2.3)) to MongoDB only, It helps later when we use the command prompt ( a shorter version ).

Open a command line console, navigate to to MongoDb\bin  & run mongod, ( primary process to connect with MongoDB server instance )

Open a new command line console, navigate to to MongoDb\bin  & run mongo, ( a shell interface to MongoDB )

*Our shell interface should be connected to server listening on port 27017 ( default port )

some .NET code :

MongoDB Team provides Several driver to connect with MongoDB System.  Download C# driver.

Open Visual Studio 2010 , create new windows forms application.

Add project Reference to MongoDB.Driver.dll

Form1

Go to code behind & add namespaces

# region MongoRefs
 using MongoDB;
 using MongoDB.Driver;
 using MongoDB.Driver.Linq;
 using MongoDB.Driver.Builders;
 #endregion

Insert Operation :

I used following code to populate some data in our mongodb datastore.

MongoServer mongoServer = MongoServer.Create();
mongoServer.Connect();
MongoDatabase mDB = mongoServer.GetDatabase("Analytics");
MongoCollection mCollection = mDB.GetCollection<DemoStats>("DemoStats");
#region Populate manual data
if (mCollection.Count() == 0)
{
DemoStats objDemoStat = new DemoStats() { SpeakerName = "Sarfraz", Id = Guid.NewGuid().ToString(), Topic = "Demonstrating MongoDB & Twitter with Web API", AudienceCount = "11" };
mCollection.Save(objDemoStat);
objDemoStat = new DemoStats() { SpeakerName = "Asif", Id = Guid.NewGuid().ToString(), Topic = "Overview Microsoft Dynamics CRM", AudienceCount = "13" };
mCollection.Save(objDemoStat);
objDemoStat = new DemoStats() { SpeakerName = "Fahad", Id = Guid.NewGuid().ToString(), Topic = "OData Services with WCF Data Services", AudienceCount = "15" };
mCollection.Save(objDemoStat);}
#endregion

You can see via Mongo Console how our data looks like.

MongoConsole

Read Operation :

I used following code to retrieve/query database records, This code applies a filter & uses the SpeakerName Field for data retrieval.

#region Retrieve Data from Mongo

if (Convert.ToInt32(mCollection.GetStats().ObjectCount) > 0)
 {

var demoData = mCollection.FindAs(typeof(DemoStats), Query.EQ("SpeakerName", txtSpeakerName.Text));
 if (demoData.Count() > 0)
 {
 foreach (DemoStats objStat in demoData)
 {
 lblTopic.Text = Convert.ToString(objStat.Topic);
 lblAudienceCount.Text = Convert.ToString(objStat.AudienceCount);
 }

lblMessage.Text = "Match Found";
 }
 else
 {
 lblMessage.Text = "Match Not Found";
 }
 }
 else
 {
 lblMessage.Text = "No Records Found";
 }
 #endregion

In the end, make sure your code disconnects from Mongo Instance

mongoServer.Disconnect();

So this is it. A couple of simple data operations to help us get acquainted with NoSQL.