From b36a769b11b1a68ba2be37781610badd68029bf1 Mon Sep 17 00:00:00 2001 From: Jovan Popovic Date: Tue, 25 Oct 2016 10:33:10 -0700 Subject: [PATCH] Connection string moved to config Connection strings in Todo REST API is moved to settings file. --- .../dotnet-react-jquery-app/README.md | 2 +- .../json/todo-app/dotnet-rest-api/.gitignore | 5 ++- .../json/todo-app/dotnet-rest-api/README.md | 35 +++++++++++++++---- .../json/todo-app/dotnet-rest-api/Startup.cs | 3 +- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/samples/features/json/comments/dotnet-react-jquery-app/README.md b/samples/features/json/comments/dotnet-react-jquery-app/README.md index 9c1fcc60..fef0dc33 100644 --- a/samples/features/json/comments/dotnet-react-jquery-app/README.md +++ b/samples/features/json/comments/dotnet-react-jquery-app/README.md @@ -60,7 +60,7 @@ If your database is hosted on Azure you can add something like: ``` { "ConnectionStrings": { - "ProductCatalog": "Server=<>.database.windows.net;Database=CommentsDb;User Id=<>;Password=<>" + "CommentsDb": "Server=<>.database.windows.net;Database=CommentsDb;User Id=<>;Password=<>" } } ``` diff --git a/samples/features/json/todo-app/dotnet-rest-api/.gitignore b/samples/features/json/todo-app/dotnet-rest-api/.gitignore index db4a5102..6ba09206 100644 --- a/samples/features/json/todo-app/dotnet-rest-api/.gitignore +++ b/samples/features/json/todo-app/dotnet-rest-api/.gitignore @@ -1,6 +1,9 @@ TodoRestWebAPI.xproj.user .vs/* +.vscode/* bin/* obj/* *.sln -*.log \ No newline at end of file +*.log +Properties/PublishProfiles/* +appsettings.development.json \ No newline at end of file diff --git a/samples/features/json/todo-app/dotnet-rest-api/README.md b/samples/features/json/todo-app/dotnet-rest-api/README.md index 181938b7..b159839c 100644 --- a/samples/features/json/todo-app/dotnet-rest-api/README.md +++ b/samples/features/json/todo-app/dotnet-rest-api/README.md @@ -39,16 +39,39 @@ To run this sample, you need the following prerequisites. ## Run this sample +### Setup + 1. From SQL Server Management Studio or Sql Server Data Tools connect to your SQL Server 2016 or Azure SQL database and execute setup.sql script that will create and populate Todo table in the database. -2. From Visual Studio, open the **TodoApp.xproj** file from the root directory, +2. From Visual Studio 2015, open the **TodoRestWebApi.xproj** file from the root directory. Restore packages using right-click menu on the project in Visual Studio and by choosing Restore Packages item. As an alternative, you may run **dotnet restore** from the command line (from the root folder of application). -3. Locate Startup.cs file in the project, change connection string in ConfigureServices method to reference your database, and build solution using Ctrl+Shift+B, right-click on project + Build, or Build/Build Solution from menu. +3. Add a connection string in appsettings.json or appsettings.development.json file. An example of the content of appsettings.development.json is shown in the following configuration: -4. Run sample app using F5 or Ctrl+F5, -4.1. Open /api/Todo Url to get all Todo items as a JSON array, -4.2. Open /api/Todo/1 Url to get details about a single Todo item with id 1, -4.3. Send POST, PUT, PATCH, or DELETE Http requests to update content of Todo table. +``` +{ + "ConnectionStrings": { + "TodoDb": "Server=.;Database=Todo;Integrated Security=true" + } +} +``` + +If your database is hosted on Azure you can add something like: +``` +{ + "ConnectionStrings": { + "TodoDb": "Server=<>.database.windows.net;Database=Todo;User Id=<>;Password=<>" + } +} +``` + +### Build and run the REST services + +1. Build solution using Ctrl+Shift+B, right-click on project + Build, Build/Build Solution from menu, or **dotnet build** command from the command line (from the root folder of application). + +2. Run sample app using F5 or Ctrl+F5, + 1. Open /api/Todo Url to get all Todo items as a JSON array, + 2. Open /api/Todo/1 Url to get details about a single Todo item with id 1, + 3. Send POST, PUT, PATCH, or DELETE Http requests to update content of Todo table. diff --git a/samples/features/json/todo-app/dotnet-rest-api/Startup.cs b/samples/features/json/todo-app/dotnet-rest-api/Startup.cs index 9be6c720..eefc93b1 100644 --- a/samples/features/json/todo-app/dotnet-rest-api/Startup.cs +++ b/samples/features/json/todo-app/dotnet-rest-api/Startup.cs @@ -26,8 +26,7 @@ namespace TodoApp // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - //const string ConnString = "Server=SERVERNAME.database.windows.net;Database=DATABASENAME;User Id=USERNAME;Password=PASSWORD"; - const string ConnString = "Server=.;Database=Todo;Integrated Security=true"; + string ConnString = Configuration["ConnectionStrings:TodoDb"]; services.AddTransient( _=> new QueryPipe(new SqlConnection(ConnString))); services.AddTransient( _=> new Command(new SqlConnection(ConnString)));