Introduction
This article shows how to use a mongoDB to communicate with ASP.NET. We will create a simple MVC application that retrieves pictures from a mongoDB.
You can see my other articles of mongoDB from here.
Previous articles have provided an introduction to mongoDB, the installation of it and communicating with ASP.NET. You can get them from the following:
Let us start creating the project from scratch using the following step-by-step procedure.
- Step 1 Create a new project; open Visual Studio 2013 then click "File" -> "New" -> "Project..." then create an "ASP.NET Web Application".
In the Templates pane, select "Installed Templates" and expand the Visual C# node. Under Visual C#, select "Web". In the list of project templates, select "ASP.NET MVC Web Application". Name the project "MongoAndMVC".
In the New ASP.NET Project dialog, select the MVC template.
This creates an outline project that is configured for MVC functionality.
- Step 2 Now add mongocsharpdriver using the Library Package Manager as in the following:
- Step 3 Next, add classes for domain models. In Solution Explorer, right-click the Models folder. Select "Add", then select "Class". Name the class "MongoPictureModel".
Add the following properties to the model class:
- using MongoDB.Bson;
-
- namespace MongoAndMVC.Models
- {
- public class MongoPictureModel
- {
- public class MongoPictureModel
- {
- public ObjectId _Id { get; set; }
-
- public string FileName { get; set; }
-
- public string PictureDataAsString { get; set; }
- }
- }
- }
- Step 4 Now add the controller as in the following:
We have added some new namespaces from the Mongo driver library to help us access the mongoDB and to provide us access to some very useful classes for manipulating the mongoDB data.
MongoAndMVC.Models;
MongoDB.Bson;
MongoDB.Driver;
MongoDB.Driver.Builders;
We also added the "System.IO" namespace for some of the byte manipulation that we'll be doing.
Now add the index view and add an image view in the imagegallery folder with the following implementation:
- @model List<MongoAndMVC.Models.MongoPictureModel>
- @{
- ViewBag.Title = "Gallery";
- }
-
- <h2>Image Gallery</h2>
-
- @Html.ActionLink("Add a new Picture", "AddPicture")
-
- @foreach (var image in Model)
- {
- <div>
- @image.FileName<br />
- <img src="@string.Format("/Gallery/ShowPicture/{0}", image._id.ToString())" alt="@image.FileName" />
- </div>
- }
- @{
- ViewBag.Title = "Upload an image";
- }
-
- <h2>Upload a new image</h2>
- @Html.ActionLink("View Gallery", "Index")
-
- <div>
- @if (ViewBag.Message != null && ViewBag.Message != "")
- {
- @ViewBag.Message
- }
- </div>
-
- <form action="" method="post" enctype="multipart/form-data">
- <input type="file" id="theFile" name="theFile" />
- <br />
- <button type="submit">Upload Image</button>
- </form>
Also edit the layout.cshtml file in the shared folder under views and add a link to the index action of the gallery controller in the app's navigation.
Let's check the output by uploading our vulpes image.
Summary
In this article we saw how to use MongoDB with an ASP.NET MVC application. I hope you have understood.