Hope it helps somebody!
|A MongoDB Document: Image courtesy (MongoDB Documentation)|
|MongoDB Collection: Image courtesy (MongoDB Documentation)|
The easiest and most popular is through Nuget. If you’re using Visual Studio, then right-click on your project in Solution Explorer and click “Manage Nuget Packages…” then search for “mongo” under the online tab and you’ll see “Official mongoDB C# Driver” listed on top. Click “Install” and wait for the installation to finish.If you use the Package Manager Console then you may want to install it using this command:Install-Package mongocsharpdriver
- There’s also a Zip archive with every release if you want to download them.
- Also there’s an MSI installer in case you want to do a Windows style installation from a setup file.
With this article I’m starting a step-by-step series on how to use MongoDB with ASP.NET MVC. In the course we will go from the basic to the advanced operations of MongoDB and gradually how to use it with ASP.NET MVC.
What you will learn
- You will learn the basics of MongoDB administration.
- You’ll learn how the .Net driver is organized and how to use it to connect to the Mongo Server.
- You’ll learn the fundamentals of how .Net types are serialized and stored in Mongo and how to customize the process to fit your specific needs.
- You’ll learn how to create an update and remove data from your MVC Applications.
- How to build models with rich behavior without the overhead of mapping to our relational model.
- You’ll learn how to use both LINQ and Mongo’s Query document style to query information from your application and how to combine them to build the most flexible of queries.
- You’ll learn how to use GridFS to store files and associate them with documents in your MVC applications.
- You’ll also learn how to avoid common pitfalls.
- Faster Development: unlike relational systems Mongo doesn’t require you to explicitly create a database, tables and columns; it’s all handled automatically by the very nature of flexible schemas. It significantly cuts the development effort.
- Minimal Migrations: Mongo also minimizes data migrations.
- Easier Collaborations: Fewer migrations mean much less pain sharing and merging changes with other developers.
- Maintainable Design: With relational storage, building a rich model requires a lot of overhead to stitch together the results from many tables and then map that to separate models in your applications. This level of overhead either isn’t justifiable or is just too difficult to maintain. This leads developers to constrain the code to fit the relational model resulting in highly procedural code, duplication, foreign key identifiers, cascading deletes and so on, scrambling all the business logic. All of this results in an application that is very difficult to maintain.
- Rich Behavior: As you start to create rich data models, you will find yourself adding rich behavior to those models as well. For example when order items are contained in an Order model, it’s easier to have the Order model calculate the total instead of doing the center Controller or otherwise.
- Scalability: Mongo Architecture allows you to scale as your traffic increases.
- GridFS: Mongo also has a fantastic system, GridFS, for storing querying and associating files with data in your application.
- Start a command prompt (cmd)
- Navigate to the MongoDB folder that you’ve unzipped.
- Enter “mongod.exe –dbpath db” , and hit Enter.
You must have noticed that I didn’t specify any port but still it’s connected. It uses the default server (localhost) and port 27017 to connect to the MongoDB server if you don’t specify any server address and port. The default database it will try to connect to is “test”.
Make sure the MongoDB server is running, otherwise the client will not be able to communicate and eventually will throw an error that it couldn’t connect to the MongoDB server:
How I solved it:
Later i the process I came to know that I had the name for an Action was “View” that you can see in the picture below.
I renamed it to View1 and it worked again. Hope this helps somebody. Thanks!
String path = HttpContext.Current.Server.MapPath(“~/myFolder/myFile.txt”);
You can use the way mentioned above but ASP.NET MVC 4 has another way to map files on server and that is using HostingEnvironment class. Actually Server.MapPath() also internally calls HostingEnvironment.MapPath() to map the file paths on server. It belongs to the System.Web.Hosting namespace. SO you can use it like this:
String path = HostingEnvironment.MapPath(“~/myFolder/myFile.txt”);
It is more recommended because its static and not dependent on current context unless you want it to use.