{"id":492,"date":"2017-01-19T17:06:59","date_gmt":"2017-01-19T15:06:59","guid":{"rendered":"https:\/\/qappdesign.com\/code\/?p=492"},"modified":"2017-01-19T21:32:24","modified_gmt":"2017-01-19T19:32:24","slug":"mongodb-aspnetmvc-core","status":"publish","type":"post","link":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/","title":{"rendered":"Using MongoDB .NET Driver with ASP.NET Core MVC"},"content":{"rendered":"<h4>Before starting<\/h4>\n<p>This is a conversion of the original article <a href=\".\/using-mongodb-with-net-core-webapi\/\" target=\"_blank\">Using MongoDB .NET Driver with .NET Core WebAPI<\/a> to ASP.NET MVC. My intention would be to not repeat any of the points discussed there, and rather focus on how to have an ASP.NET Web App running. <\/p>\n<h4>To install<\/h4>\n<p>Here are all the things needed to be installed:<\/p>\n<ul>\n<li><a href=\"http:\/\/visualstudio.com\/free\" target=\"_blank\">Visual Studio Community 2015<\/a> and then <a href=\"https:\/\/go.microsoft.com\/fwlink\/?LinkId=691129\" target=\"_blank\">Visual Studio 2015 Update 3<\/a> and <a href=\"https:\/\/go.microsoft.com\/fwlink\/?LinkID=827546\" target=\"_blank\">.NET Core 1.0.1 &#8211; VS 2015 Tooling Preview 2<\/a><\/li>\n<li><a href=\"https:\/\/www.mongodb.com\/download-center#community\" target=\"_blank\">MongoDB<\/a> and <a href=\"https:\/\/robomongo.org\/\" target=\"_blank\">Robomongo<\/a><\/li>\n<\/ul>\n<h4>Project available in GitHub<\/h4>\n<p>Full source of this example is available on GitHub -> <a href=\"https:\/\/github.com\/fpetru\/mongodb-aspnetmvc\" target=\"_blank\">https:\/\/github.com\/fpetru\/mongodb-aspnetmvc<\/a>. It includes creation of an ASP.NET Core Web Application, as well as copy of the existent code written. The project was creating by launching Visual Studio and\u00a0then accessing menu: File\u00a0>\u00a0New Project\u00a0>\u00a0.Net Core\u00a0>\u00a0ASP.NET Core Web Application.<\/p>\n<h4>Run the sample project<\/h4>\n<p>After you have installed the required items, you would just need to have MongoDb server running, and to define the user credentials, to be able to access MongoDB. Once you have the credentials, write these settings in <em>appsettings.json<\/em>. You can find details on how to do this in the original article <a href=\".\/using-mongodb-with-net-core-webapi\/\" target=\"_blank\">Using MongoDB .NET Driver with .NET Core WebAPI<\/a>. <\/p>\n<p>Then just compile and run from Visual Studio. From command line would be<\/p>\n<pre>\r\ndotnet restore\r\ndotnet run\r\n<\/pre>\n<p>The page displayed in browser will look<br \/>\n<img src=\"data:image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\" data-src=\"https:\/\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg\" alt=\"\" class=\"lazyload \" \/><noscript><img src=\"https:\/\/i0.wp.com\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg?w=960&#038;ssl=1\" alt=\"\" data-recalc-dims=\"1\" \/><\/noscript><\/p>\n<h4>Controller actions<\/h4>\n<p>Basically the change done is just in controller file (HomeController.cs), adding these actions.<\/p>\n<pre>\r\npublic async Task<IActionResult> Read()\r\n{\r\n\tconst string nodeId = \"2\";\r\n\tNote noteElement = await _noteRepository.GetNote(nodeId) ?? new Note();\r\n\tViewData[\"Message\"] = string.Format($\"Note Id: {nodeId} - Body: {noteElement.Body}\");\r\n\r\n\treturn View();\r\n}\r\n\r\npublic IActionResult Init()\r\n{\r\n\t_noteRepository.RemoveAllNotes();\r\n\t_noteRepository.AddNote(new Note() { Id = \"1\", Body = \"Test note 1\", CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, UserId = 1 });\r\n\t_noteRepository.AddNote(new Note() { Id = \"2\", Body = \"Test note 2\", CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, UserId = 1 });\r\n\t_noteRepository.AddNote(new Note() { Id = \"3\", Body = \"Test note 3\", CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, UserId = 2 });\r\n\t_noteRepository.AddNote(new Note() { Id = \"4\", Body = \"Test note 4\", CreatedOn = DateTime.Now, UpdatedOn = DateTime.Now, UserId = 2 });\r\n\r\n\tViewData[\"Message\"] = string.Format($\"Filled in 4 records\");\r\n\treturn View();\r\n}\r\n<\/pre>\n<h4>Unified frameworks<\/h4>\n<p>This conversion was very simple, due to fact that ASP.NET Core has unified the two frameworks, making it easy to build applications that include both UI (HTML) and APIs. Since they share the same code base and pipeline, the update was done just in the data is display, in the controller class.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Before starting This is a conversion of the original article Using MongoDB .NET Driver with .NET Core WebAPI to ASP.NET MVC. My intention would be to not repeat any of the points discussed there, and rather focus on how to have an ASP.NET Web App running. To install Here are all the things needed to be installed: Visual Studio Community 2015 and then Visual Studio 2015 Update 3 and .NET Core 1.0.1 &#8211; VS 2015 Tooling Preview 2 MongoDB and Robomongo Project available in GitHub Full source of this example is available on GitHub -> https:\/\/github.com\/fpetru\/mongodb-aspnetmvc. It includes creation of an ASP.NET Core Web Application, as well as copy of the existent code written. The project was creating by launching Visual Studio and\u00a0then accessing menu: File\u00a0>\u00a0New Project\u00a0>\u00a0.Net Core\u00a0>\u00a0ASP.NET Core Web Application. Run the sample project After you have installed the required items, you would just need to have MongoDb server running, and to define the user credentials, to be able to access MongoDB. Once you have the credentials, write these settings in appsettings.json. You can find details on how to do this in the original article Using MongoDB .NET Driver with .NET Core WebAPI. Then just compile and run from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"Using MongoDB .NET Driver with ASP.NET Core MVC","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[13,6],"tags":[3],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v18.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using MongoDB .NET Driver with ASP.NET Core MVC - Cloud, Data and Integrations<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using MongoDB .NET Driver with ASP.NET Core MVC - Cloud, Data and Integrations\" \/>\n<meta property=\"og:description\" content=\"Before starting This is a conversion of the original article Using MongoDB .NET Driver with .NET Core WebAPI to ASP.NET MVC. My intention would be to not repeat any of the points discussed there, and rather focus on how to have an ASP.NET Web App running. To install Here are all the things needed to be installed: Visual Studio Community 2015 and then Visual Studio 2015 Update 3 and .NET Core 1.0.1 &#8211; VS 2015 Tooling Preview 2 MongoDB and Robomongo Project available in GitHub Full source of this example is available on GitHub -&gt; https:\/\/github.com\/fpetru\/mongodb-aspnetmvc. It includes creation of an ASP.NET Core Web Application, as well as copy of the existent code written. The project was creating by launching Visual Studio and\u00a0then accessing menu: File\u00a0&gt;\u00a0New Project\u00a0&gt;\u00a0.Net Core\u00a0&gt;\u00a0ASP.NET Core Web Application. Run the sample project After you have installed the required items, you would just need to have MongoDb server running, and to define the user credentials, to be able to access MongoDB. Once you have the credentials, write these settings in appsettings.json. You can find details on how to do this in the original article Using MongoDB .NET Driver with .NET Core WebAPI. Then just compile and run from [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/\" \/>\n<meta property=\"og:site_name\" content=\"Cloud, Data and Integrations\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-19T15:06:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-01-19T19:32:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@qappdesign\" \/>\n<meta name=\"twitter:site\" content=\"@QAppDesign\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Petru Faurescu\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/qappdesign.com\/code\/#website\",\"url\":\"https:\/\/qappdesign.com\/code\/\",\"name\":\"QualityAppDesign\",\"description\":\"with Petru Faurescu\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/qappdesign.com\/code\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/i0.wp.com\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg?fit=594%2C296&ssl=1\",\"contentUrl\":\"https:\/\/i0.wp.com\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg?fit=594%2C296&ssl=1\",\"width\":594,\"height\":296},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#webpage\",\"url\":\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/\",\"name\":\"Using MongoDB .NET Driver with ASP.NET Core MVC - Cloud, Data and Integrations\",\"isPartOf\":{\"@id\":\"https:\/\/qappdesign.com\/code\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#primaryimage\"},\"datePublished\":\"2017-01-19T15:06:59+00:00\",\"dateModified\":\"2017-01-19T19:32:24+00:00\",\"author\":{\"@id\":\"https:\/\/qappdesign.com\/code\/#\/schema\/person\/54db90dc6fe846cfd4c5a9544d93b75a\"},\"breadcrumb\":{\"@id\":\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/qappdesign.com\/code\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using MongoDB .NET Driver with ASP.NET Core MVC\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/qappdesign.com\/code\/#\/schema\/person\/54db90dc6fe846cfd4c5a9544d93b75a\",\"name\":\"Petru Faurescu\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/qappdesign.com\/code\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/84fb359a4e3d583dbea5a34bd5566956?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/84fb359a4e3d583dbea5a34bd5566956?s=96&d=mm&r=g\",\"caption\":\"Petru Faurescu\"},\"description\":\"Product lead, software developer &amp; architect\",\"sameAs\":[\"https:\/\/qappdesign.com\/code\",\"https:\/\/www.linkedin.com\/in\/petrufaurescu\/\",\"https:\/\/twitter.com\/@qappdesign\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using MongoDB .NET Driver with ASP.NET Core MVC - Cloud, Data and Integrations","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/","og_locale":"en_US","og_type":"article","og_title":"Using MongoDB .NET Driver with ASP.NET Core MVC - Cloud, Data and Integrations","og_description":"Before starting This is a conversion of the original article Using MongoDB .NET Driver with .NET Core WebAPI to ASP.NET MVC. My intention would be to not repeat any of the points discussed there, and rather focus on how to have an ASP.NET Web App running. To install Here are all the things needed to be installed: Visual Studio Community 2015 and then Visual Studio 2015 Update 3 and .NET Core 1.0.1 &#8211; VS 2015 Tooling Preview 2 MongoDB and Robomongo Project available in GitHub Full source of this example is available on GitHub -> https:\/\/github.com\/fpetru\/mongodb-aspnetmvc. It includes creation of an ASP.NET Core Web Application, as well as copy of the existent code written. The project was creating by launching Visual Studio and\u00a0then accessing menu: File\u00a0>\u00a0New Project\u00a0>\u00a0.Net Core\u00a0>\u00a0ASP.NET Core Web Application. Run the sample project After you have installed the required items, you would just need to have MongoDb server running, and to define the user credentials, to be able to access MongoDB. Once you have the credentials, write these settings in appsettings.json. You can find details on how to do this in the original article Using MongoDB .NET Driver with .NET Core WebAPI. Then just compile and run from [&hellip;]","og_url":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/","og_site_name":"Cloud, Data and Integrations","article_published_time":"2017-01-19T15:06:59+00:00","article_modified_time":"2017-01-19T19:32:24+00:00","og_image":[{"url":"https:\/\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg"}],"twitter_card":"summary","twitter_creator":"@qappdesign","twitter_site":"@QAppDesign","twitter_misc":{"Written by":"Petru Faurescu","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"https:\/\/qappdesign.com\/code\/#website","url":"https:\/\/qappdesign.com\/code\/","name":"QualityAppDesign","description":"with Petru Faurescu","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/qappdesign.com\/code\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#primaryimage","inLanguage":"en-US","url":"https:\/\/i0.wp.com\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg?fit=594%2C296&ssl=1","contentUrl":"https:\/\/i0.wp.com\/qappdesign.com\/code\/wp-content\/uploads\/2017\/01\/ASP.NET-Core-MVC-Sample-Application-1.jpg?fit=594%2C296&ssl=1","width":594,"height":296},{"@type":"WebPage","@id":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#webpage","url":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/","name":"Using MongoDB .NET Driver with ASP.NET Core MVC - Cloud, Data and Integrations","isPartOf":{"@id":"https:\/\/qappdesign.com\/code\/#website"},"primaryImageOfPage":{"@id":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#primaryimage"},"datePublished":"2017-01-19T15:06:59+00:00","dateModified":"2017-01-19T19:32:24+00:00","author":{"@id":"https:\/\/qappdesign.com\/code\/#\/schema\/person\/54db90dc6fe846cfd4c5a9544d93b75a"},"breadcrumb":{"@id":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/qappdesign.com\/code\/mongodb-aspnetmvc-core\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/qappdesign.com\/code\/"},{"@type":"ListItem","position":2,"name":"Using MongoDB .NET Driver with ASP.NET Core MVC"}]},{"@type":"Person","@id":"https:\/\/qappdesign.com\/code\/#\/schema\/person\/54db90dc6fe846cfd4c5a9544d93b75a","name":"Petru Faurescu","image":{"@type":"ImageObject","@id":"https:\/\/qappdesign.com\/code\/#personlogo","inLanguage":"en-US","url":"https:\/\/secure.gravatar.com\/avatar\/84fb359a4e3d583dbea5a34bd5566956?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/84fb359a4e3d583dbea5a34bd5566956?s=96&d=mm&r=g","caption":"Petru Faurescu"},"description":"Product lead, software developer &amp; architect","sameAs":["https:\/\/qappdesign.com\/code","https:\/\/www.linkedin.com\/in\/petrufaurescu\/","https:\/\/twitter.com\/@qappdesign"]}]}},"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p82xvM-7W","_links":{"self":[{"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/posts\/492"}],"collection":[{"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/comments?post=492"}],"version-history":[{"count":15,"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/posts\/492\/revisions"}],"predecessor-version":[{"id":509,"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/posts\/492\/revisions\/509"}],"wp:attachment":[{"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/media?parent=492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/categories?post=492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qappdesign.com\/code\/wp-json\/wp\/v2\/tags?post=492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}