MongoDB on ASP. NET mvc3

Source: Internet
Author: User
Tags connectionstrings

I had nothing to worry about in the evening and studied MongoDB, which is very popular recently. There is only one feeling: "free ".

Let's skip this. Let MongoDB run under win7 64 first:

1. MongoDB Homepage

Http://www.mongodb.org/

2. Download

Http://downloads.mongodb.org/win32/mongodb-win32-x86_64-1.8.1.zip

3. Extract

E.g. H: \ mongodb-win32-x86_64-1.8.1

4. Prepare the Directory

E.g. H: \ Mongo \ data -- save the database file H: \ Mongo \ logs --save the logstore, and create a log.txt in this directory.

5. Install

Win key, Enter cmd, right-click-run as Administrator

Cd h: \ mongodb-win32-x86_64-1.8.1 \ bin

H:

Mongod -- bind_ip 127.0.0.1 -- logpath H: \ Mongo \ logs \ log.txt -- logappend -- dbpath H: \ Mongo \ data -- directoryperdb-install

Net start "MongoDB"

Successful.

6. test and install

In the previous cmd window, enter Mongo

Enter the query interface without exception

Enter 3 + 3 and get 6

Turn Off cmd. This is useless.

7. Download. Net Connector

Https://github.com/mongodb/mongo-csharp-driver/archives/master

Extract

Open CSharpDriverSetup-2010.sln

Build

Failed

Delete the CHM reference in driversetup

Re-compilation successful

Right-click driversetup-install

Next, click Next...

8. Create an ASP. NET mvc3 Empty Project

If not, install the mvc3 tool on your own.

9. Add connectionstring in Web. config

< Connectionstrings >
< Add Name = "MongoDB" Connectionstring = "MongoDB: // localhost/test" />
 
</Connectionstrings> 

10. Add Controller

Controller with empty read/write actions

11. Access to the DB

Private Relational Database getdb ()
{
Return Relational Database. Create (configurationmanager. connectionstrings [ " MongoDB " ]. Connectionstring );

 

12. Create action

 
 [Httppost]
Public Actionresult create (formcollection collection)
{
Try
{
VaR DB = Getdb ();
Foreach (Var key In Collection. allkeys)
{
DB [ " Testtable " ]. Insert ( New MongoDB. bson. bsondocument {
{Key, Collection [Key]}
});
}

return redirecttoaction ( " index " );
}< br> catch
{< br> return View ();
}< BR >}

13. Index action
   Public  Actionresult index ()
{
Try
{
VaR DB = Getdb ();

VaR testtable = DB [ " Testtable " ]. Findall ();
VaR result =   New Stringbuilder ();
Foreach (VAR testdata In Testtable)
{
Foreach (VAR Property In Testdata. Names)
{
Result. appendformat ( " {0 }:{ 1} " , Property, testdata [property]);
}

Result. append ( " <Br/> " );
}

Return Content (result. tostring ());
}
Catch
{
Return View ();
}
}
 
14. Create views under views/home/
 
Create. cshtml:
 @{
Viewbag. Title = "CREATE ";
Layout = "~ /Views/shared/_ layout. cshtml ";
}

< H2 > Create </ H2 >

< Form Method = "Post" Action = "/Home/create" >
Name:
< Input Name = "Name" Type = "Text"   /> < BR />
Age:
< Input Name = "Age" Type = "Text"   /> < BR />
Gender:
< Input Name = "Gender" Type = "Text"   /> < BR />
Married:
< Input Name = "Married" Type = "Text"   /> < BR />
< Input Type = "Submit" Value = "Add"   />
</ Form >
 
Index. cshtml
  {< br> viewbag. Title = "Index"; 
layout = "~ /Views/shared/_ layout. cshtml ";
}< br> H2 > index H2 >
A href =" CREATE " > Create New A >
15. modify global. asax. CS
 Routes. maproute (
" Default " , // Route name
" {Controller}/{action}/{ID} " , // URL with Parameters
New {Controller =   " Home " , Action =   " Create " , ID = Urlparameter. optional} // Parameter defaults
);
 
11. Build & run
 
 
 
 

Press add

 

All done. Happy and enjoy.

 

Download the makeup project:

Http://files.cnblogs.com/pandora/MvcApplication1.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.