Skip to content

Commit 4c8d40f

Browse files
committed
updated readme
1 parent a849b23 commit 4c8d40f

File tree

1 file changed

+118
-24
lines changed

1 file changed

+118
-24
lines changed

README.md

Lines changed: 118 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
---
22
page_type: sample
33
languages:
4-
- csharp
4+
- tsql
5+
- sql
56
products:
6-
- dotnet
7-
description: "Add 150 character max description"
8-
urlFragment: "update-this-to-unique-url-stub"
7+
- azure
8+
- donet
9+
- aspnet
10+
= dotnet-core
11+
- aspnet-core
12+
- azure-api-apps
13+
- vs-code
14+
- azure-sql-database
15+
description: "Creating a modern REST API with .NET Core and Azure SQL, using Dapper and Visual Studio Code"
16+
urlFragment: "azure-sql-db-python-rest-api"
917
---
1018

11-
# Official Microsoft Sample
19+
# Creating a REST API with .NET Core and Azure SQL
1220

1321
<!--
1422
Guidelines on README format: https://review.docs.microsoft.com/help/onboard/admin/samples/concepts/readme-template?branch=master
@@ -18,36 +26,122 @@ Guidance on onboarding samples to docs.microsoft.com/samples: https://review.doc
1826
Taxonomies for products and languages: https://review.docs.microsoft.com/new-hope/information-architecture/metadata/taxonomies?branch=master
1927
-->
2028

21-
Give a short description for your sample here. What does it do and why is it important?
29+
Thanks to native JSON support, creating a REST API with Azure SQL and .NET Core is really a matter of a few lines of code. Take a look at `app.py` to easy it is!
2230

23-
## Contents
31+
Wondering what's the magic behind? The sample uses the well known [Flask](https://flask.palletsprojects.com/en/1.1.x/) micro-framework and the [flask-restful](https://flask-restful.readthedocs.io/en/latest/) package to easily implement REST APIs. Beside that the [native JSON support that Azure SQL provides](https://docs.microsoft.com/en-us/azure/sql-database/sql-database-json-features) does all the heavy lifting so sending data back and forth to the database is as easy as sending a JSON message.
2432

25-
Outline the file contents of the repository. It helps users navigate the codebase, build configuration and any related assets.
33+
## Install Sample Database
2634

27-
| File/folder | Description |
28-
|-------------------|--------------------------------------------|
29-
| `src` | Sample source code. |
30-
| `.gitignore` | Define what to ignore at commit time. |
31-
| `CHANGELOG.md` | List of changes to the sample. |
32-
| `CONTRIBUTING.md` | Guidelines for contributing to the sample. |
33-
| `README.md` | This README file. |
34-
| `LICENSE` | The license for the sample. |
35+
In order to run this sample, the WideWorldImporters database is needed. Install WideWorldImporters sample database:
3536

36-
## Prerequisites
37+
[Restore WideWorldImporters Database](https://github.com/yorek/azure-sql-db-samples#restore-wideworldimporters-database)
3738

38-
Outline the required components and tools that a user might need to have on their machine in order to run the sample. This can be anything from frameworks, SDKs, OS versions or IDE releases.
39+
## Add Database Objects
3940

40-
## Setup
41+
Once the sample database has been installed, you need to add some stored procedure that will called from Python. The SQL code is available here:
4142

42-
Explain how to prepare the sample once the user clones or downloads the repository. The section should outline every step necessary to install dependencies and set up any settings (for example, API keys and output folders).
43+
`./SQL/WideWorldImportersUpdates.sql`
4344

44-
## Running the sample
45+
If you need any help in executing the SQL script, you can find a Quickstart here: [Quickstart: Use Azure Data Studio to connect and query Azure SQL database](https://docs.microsoft.com/en-us/sql/azure-data-studio/quickstart-sql-database)
4546

46-
Outline step-by-step instructions to execute the sample and see its output. Include steps for executing the sample from the IDE, starting specific services in the Azure portal or anything related to the overall launch of the code.
47+
## Run sample locally
4748

48-
## Key concepts
49+
Make sure you have [.NET Core 3.0](https://dotnet.microsoft.com/download) SDK installed on your machine. Clone this repo in a directory on our computer and then configure the connection string in `appsettings.json`.
4950

50-
Provide users with more context on the tools and services used in the sample. Explain some of the code that is being used and how services interact with each other.
51+
If you don't want to save the connection string in the `appsettings.json` file for security reasons, you can just set it using an environment variable:
52+
53+
Linux:
54+
55+
```bash
56+
export ConnectionStrings__DefaultConnection="<your-connection-string>"
57+
```
58+
59+
Windows:
60+
61+
```powershell
62+
$Env:ConnectionStrings__DefaultConnection="<your-connection-string>"
63+
```
64+
65+
Your connection string is something like:
66+
67+
```
68+
DRIVER={ODBC Driver 17 for SQL Server};SERVER=<your-server-name>.database.windows.net;DATABASE=<your-database-name>;UID=DotNetWebApp;PWD=a987REALLY#$%TRONGpa44w0rd
69+
```
70+
71+
Just replace `<your-server-name>` and `<your-database-name>` with the correct values for your environment.
72+
73+
To run and test the REST API locally, just run
74+
75+
```bash
76+
dotnet run
77+
```
78+
79+
.NET will start the HTTP server and when everything is up and running you'll see something like
80+
81+
```text
82+
Now listening on: https://localhost:5001
83+
```
84+
85+
Using a REST Client (like [Insomnia](https://insomnia.rest/), [Postman](https://www.getpostman.com/) or curl), you can now call your API, for example:
86+
87+
```bash
88+
curl -k -X GET http://localhosts:5001/customer/123
89+
```
90+
91+
and you'll get info on Customer 123:
92+
93+
```json
94+
[
95+
{
96+
"CustomerID": 123,
97+
"CustomerName": "Tailspin Toys (Roe Park, NY)",
98+
"PhoneNumber": "(212) 555-0100",
99+
"FaxNumber": "(212) 555-0101",
100+
"WebsiteURL": "http://www.tailspintoys.com/RoePark",
101+
"Delivery": {
102+
"AddressLine1": "Shop 219",
103+
"AddressLine2": "528 Persson Road",
104+
"PostalCode": "90775"
105+
}
106+
}
107+
]
108+
```
109+
110+
Check out more samples to test all implemented verbs here:
111+
112+
[cUrl Samples](./Sample-Usage.md)
113+
114+
## Deploy to Azure
115+
116+
Now that your REST API solution is ready, it's time to deploy it on Azure so that anyone can take advantage of it. A detailed article on how you can that that is here:
117+
118+
- [Deploying Python web apps to Azure App Services](https://medium.com/@GeekTrainer/deploying-python-web-apps-to-azure-app-services-413cc16d4d68)
119+
- [Quickstart: Create a Python app in Azure App Service on Linux](https://docs.microsoft.com/en-us/azure/app-service/containers/quickstart-python?tabs=bash)
120+
121+
The only thing you have do in addition to what explained in the above articles is to add the connection string to the Azure Web App configuration. Using AZ CLI, for example:
122+
123+
```bash
124+
appName="azure-sql-db-dotnet-rest-api"
125+
resourceGroup="my-resource-group"
126+
127+
az webapp config connection-string set \
128+
-g $resourceGroup \
129+
-n $appName \
130+
--settings DefaultConnection=$ConnectionStrings__DefaultConnection \
131+
--connection-string-type=SQLAzure
132+
```
133+
134+
Just make sure you correctly set `$appName` and `$resourceGroup` to match your environment and also that the variable `$ConnectionStrings__DefaultConnection` as also been set, as mentioned in section "Run sample locally". An example of a full script that deploy the REST API is available here: `azure-deploy.sh`.
135+
136+
## Learn more
137+
138+
If you're new to .NET and want to learn more, there are a lot of tutorial available on the [Microsoft Learn](https://docs.microsoft.com/en-us/learn/browse/?products=dotnet) platform. You can start from here, for example:
139+
140+
- https://docs.microsoft.com/en-us/learn/modules/build-web-api-net-core/?view=aspnetcore-3.1
141+
142+
If you also want to learn more about Visual Studio Code, here's another resource:
143+
144+
[Using .NET Core in Visual Studio Code](https://code.visualstudio.com/docs/languages/dotnet)
51145

52146
## Contributing
53147

0 commit comments

Comments
 (0)