{"id":75937,"date":"2020-09-25T18:00:10","date_gmt":"2020-09-25T15:00:10","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it\/"},"modified":"2020-09-25T18:00:10","modified_gmt":"2020-09-25T15:00:10","slug":"how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it\/","title":{"rendered":"#How to Get Started with ASP.NET for API Development \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a40a96eae7f4\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a40a96eae7f4\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it\/#Creating_a_Project\" >Creating a Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it\/#Connecting_a_Database\" >Connecting a Database<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it\/#Writing_a_CRUD_Database_Service\" >Writing a CRUD Database Service<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-to-get-started-with-asp-net-for-api-development-cloudsavvy-it\/#Setting_Up_an_API_Controller\" >Setting Up an API Controller<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How to Get Started with ASP.NET for API Development \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5197\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/815f3df36d0049dd942039e0a406b482\/p\/uploads\/2020\/06\/a448385b.png\" alt=\"asp.net\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>ASP.NET is a platform for building web APIs with C# and .NET. It\u2019s commonly used for <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a> backends, and can automatically serialize classes to JSON. We\u2019ll walk through setting up a service that talks to a database and stores a list of objects.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Creating_a_Project\"><\/span>Creating a Project<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We\u2019ll be using <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fvisualstudio.microsoft.com%2Fvs%2F&amp;key=204a528a336ede4177fff0d84a044482\">Visual Studio<\/a>, as it provides great .NET support out of the box. Create a new project using the \u201cASP.NET Core Web Application\u201d template:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5186\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/674fa982f4c5be9249ea9c45dacb961c\/p\/uploads\/2020\/06\/721ef15f.png\" alt=\"create new project\" width=\"700\" height=\"302\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Give it a name, and select \u201cAPI,\u201d as we\u2019re not building a ASP.NET frontend here.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5187\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/6c279ea1c7674d151eb1cb184806220d\/p\/uploads\/2020\/06\/be5ae268.png\" alt=\"select API\" width=\"700\" height=\"346\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>This will initialize your solution with all the boilerplate needed to get a basic API up and running. If you click the run button (labelled IIS Express, which starts an IIS web server), you\u2019ll see the API displaying some dummy data about the weather.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5188\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/ad185fa288f5aab6e074dbc6aee94825\/p\/uploads\/2020\/06\/7f046e9b.png\" alt=\"start program\" width=\"452\" height=\"135\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5189\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/fa31cc2d5bc4f70cd2d70673548d23bb\/p\/uploads\/2020\/06\/75e840fb.png\" alt=\"dummy data\" width=\"700\" height=\"300\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>We\u2019ll use this base to set up a simple API that will connect to a database and read and write custom objects. In this case, it\u2019s a collection of user data, but the API will largely work the same for more complicated entity models.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Connecting_a_Database\"><\/span>Connecting a Database<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It\u2019s a good idea to split up the logic of handling requests and the logic of handling talking to the database. We\u2019ll create a database service with some functions for reading and writing, then an a API controller that will respond to requests by talking to the database service. The first thing to do, though, is get ASP.NET talking to a database in the first place.<\/p>\n<p>We\u2019ll need to install some packages for working with a database. Click on Tools &gt; NuGet Package Manager, and select \u201cManage NuGet Packages For Solution.\u201d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5190\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/c5cb9af2f7871bd018dbee62ca39cf39\/p\/uploads\/2020\/06\/a47c9ace.png\" alt=\"install nugest packages\" width=\"700\" height=\"291\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you\u2019re using a SQL database, the database connection is handled with specific plugin packages that implement <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"https:\/\/github.com\/dotnet\/efcore\">EntityFrameworkCore<\/a>\u00a0for the underlying database. This is a framework that maps C# objects to a relational database, and allows you to use LINQ queries and other native tools to interact with it.\u00a0You can find a <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fef%2Fcore%2Fproviders%2F%3Ftabs%3Ddotnet-core-cli&amp;key=204a528a336ede4177fff0d84a044482\">full list of database providers here<\/a> for most popular relational databases.<\/p>\n<p>We\u2019ll be going with MongoDB here, because NoSQL document databases translate to a <code>List&lt;Object&gt;<\/code> pretty seamlessly, and the MongoDB driver supports LINQ queries much like EF Core.<\/p>\n<p>Install the MongoDB.Driver package:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5191 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/529754efc85972962b0dae8dc9e024d2\/p\/uploads\/2020\/06\/97b79526.png\" alt=\"Installing the MongoDB.Driver package\" width=\"700\" height=\"246\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Create a new folder at the root of your project called \u201cModels.\u201d In it, create an entity model called <code>User.cs<\/code>, which will be the schema for the collection that gets stored in the database. You can use the <code>[BsonId]<\/code>\u00a0and <code>[BsonRepresentation(BsonType.ObjectId)]<\/code> attributes to tell the MongoDB driver that this ID string should be interpreted as the collection ID, and can be passed as type <code>string<\/code>\u00a0even though Mongo internally wants type <code>ObjectId<\/code>.<\/p>\n<pre>using MongoDB.Bson;&#13;\nusing MongoDB.Bson.Serialization.Attributes;&#13;\nusing System;&#13;\n&#13;\nnamespace ExampleService.Models&#13;\n{&#13;\n    public class User&#13;\n    {&#13;\n        [BsonId]&#13;\n        [BsonRepresentation(BsonType.ObjectId)]&#13;\n        public string Id { get; set; }&#13;\n        &#13;\n        [BsonElement(\"Name\")]&#13;\n        public string Name { get; set; }&#13;\n        public string Email { get; set; }&#13;\n        \/\/ etc...&#13;\n    }&#13;\n}<\/pre>\n<p>In this same folder, create a file called <code>DatabaseSettings.cs<\/code>, which will store a class and interface for the database settings.<\/p>\n<pre>namespace ExampleService.Models&#13;\n{&#13;\n    public class ExampleDatabaseSettings : IExampleDatabaseSettings&#13;\n    {&#13;\n        public string ExampleCollectionName { get; set; }&#13;\n        public string ConnectionString { get; set; }&#13;\n        public string DatabaseName { get; set; }&#13;\n    }&#13;\n&#13;\n    public interface IExampleDatabaseSettings&#13;\n    { &#13;\n        string ExampleCollectionName { get; set; }&#13;\n        string ConnectionString { get; set; }&#13;\n        string DatabaseName { get; set; }&#13;\n    }&#13;\n}<\/pre>\n<p>We can populate these property values in <code>appsettings.json<\/code>, by adding the following configuration:<\/p>\n<pre>\"ExampleDatabaseSettings\": {&#13;\n    \"ExampleCollectionName\": \"Users\",&#13;\n    \"ConnectionString\": \"mongodb:\/\/localhost:27017\",&#13;\n    \"DatabaseName\": \"Example\"&#13;\n}<\/pre>\n<p>You will, of course, need to create or rename the <code>Example<\/code>\u00a0database, as well as create the <code>Users<\/code>\u00a0collection in it.<\/p>\n<p>Finally, we will add some code in <code>Startup.cs<\/code>\u00a0in the <code>ConfigureServices<\/code>\u00a0method, which will fetch the settings from the <code>appsettings.json<\/code>\u00a0file, and register the interface through <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Ffundamentals%2Fdependency-injection%3Fview%3Daspnetcore-3.1%23service-lifetimes&amp;key=204a528a336ede4177fff0d84a044482\">Dependency Injection<\/a>:<\/p>\n<pre>public void ConfigureServices(IServiceCollection services)&#13;\n{&#13;\n    services.Configure&lt;ExampleDatabaseSettings&gt;(&#13;\n      Configuration.GetSection(nameof(ExampleDatabaseSettings)));&#13;\n&#13;\n    services.AddSingleton&lt;IExampleDatabaseSettings&gt;(sp =&gt;&#13;\n      sp.GetRequiredService&lt;IOptions&lt;ExampleDatabaseSettings&gt;&gt;().Value);&#13;\n&#13;\n    services.AddControllers();&#13;\n}<\/pre>\n<p>You will need to add a directive at the top using your Models namespace.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Writing_a_CRUD_Database_Service\"><\/span>Writing a CRUD Database Service<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once the database is connected, you can add the service that will do the talking. The following class contains an <code>IMongoCollection<\/code>\u00a0that represents the MongoDB collection in a format with which C# can easily interact. Reads, writes, updates and deletes will directly affect the underlying collection.<\/p>\n<p>The constructor method takes the database settings, starts up a new client, fetches the database, then initializes the\u00a0<code>_users<\/code>\u00a0collection.<\/p>\n<pre><span class=\"hljs-keyword\">using<\/span> ExampleService.Models;&#13;\n<span class=\"hljs-keyword\">using<\/span> MongoDB.Driver;&#13;\n<span class=\"hljs-keyword\">using<\/span> System.Collections.Generic;&#13;\n<span class=\"hljs-keyword\">using<\/span> System.Linq;&#13;\n&#13;\n<span class=\"hljs-keyword\">namespace<\/span> ExampleService<span class=\"hljs-title\">.Services<\/span>&#13;\n{&#13;\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> ExampleService&#13;\n    {&#13;\n        <span class=\"hljs-keyword\">private<\/span> <span class=\"hljs-keyword\">readonly<\/span> IMongoCollection&lt;User&gt; _users;&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title\">UserService<\/span>(<span class=\"hljs-params\">IExampleDatabaseSettings settings<\/span>)<\/span>&#13;\n        {&#13;\n            <span class=\"hljs-keyword\">var<\/span> client = <span class=\"hljs-keyword\">new<\/span> MongoClient(settings.ConnectionString);&#13;\n            <span class=\"hljs-keyword\">var<\/span> database = client.GetDatabase(settings.DatabaseName);&#13;\n&#13;\n            _users = database.GetCollection&lt;User&gt;(settings.ExampleCollectionName);&#13;\n        }&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> List&lt;User&gt; <span class=\"hljs-title\">Get<\/span>()<\/span> =&gt;&#13;\n            _users.Find(user =&gt; <span class=\"hljs-literal\">true<\/span>).ToList();&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> User <span class=\"hljs-title\">Get<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> id<\/span>)<\/span> =&gt;&#13;\n            _users.Find&lt;User&gt;(user =&gt; user.Id == id).FirstOrDefault();&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> User <span class=\"hljs-title\">Create<\/span>(User user)<\/span>&#13;\n        {&#13;\n            _users.InsertOne(user);&#13;\n            <span class=\"hljs-keyword\">return<\/span> user;&#13;\n        }&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Update<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> id, User userIn<\/span>)<\/span> =&gt;&#13;\n            _users.ReplaceOne(user=&gt; user.Id == id, <span class=\"hljs-function\"><span class=\"hljs-params\">userIn<\/span><\/span>);&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Remove<\/span>(User userIn)<\/span> =&gt;&#13;\n            _users.DeleteOne(user =&gt; user.Id == userIn.Id);&#13;\n&#13;\n        <span class=\"hljs-function\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title\">Remove<\/span>(<span class=\"hljs-params\"><span class=\"hljs-keyword\">string<\/span> id<\/span>)<\/span> =&gt; &#13;\n            _users.DeleteOne(user=&gt; user.Id == id);&#13;\n    }&#13;\n}<\/pre>\n<p>The rest of the class is simply a set of CRUD operations for interacting with the database. Feel free to implement any custom logic here.<\/p>\n<p>Back in <code>Startup.cs<\/code>, you\u2019ll want to add this as a service on startup:<\/p>\n<pre>public void ConfigureServices(IServiceCollection services)&#13;\n{&#13;\n    services.Configure&lt;ExampleDatabaseSettings&gt;(&#13;\n      Configuration.GetSection(nameof(ExampleDatabaseSettings)));&#13;\n&#13;\n    services.AddSingleton&lt;IExampleDatabaseSettings&gt;(sp =&gt;&#13;\n      sp.GetRequiredService&lt;IOptions&lt;ExampleDatabaseSettings&gt;&gt;().Value);&#13;\n&#13;\n    services.AddSingleton&lt;ExampleService&gt;();&#13;\n&#13;\n    services.AddControllers();&#13;\n}<\/pre>\n<p>You\u2019ll again need another <code>using<\/code>\u00a0directive, this time for <code>ExampleService.Services<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Setting_Up_an_API_Controller\"><\/span>Setting Up an API Controller<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Next, you\u2019ll need a controller to handle API actions. Luckily Visual Studio makes this easy; you can right-click on \u201cControllers\u201d to add a new controller, with read\/write actions. This will create a new controller with some basic methods.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"imgchk9 alignnone wp-image-5127 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/af5084099e5c5d4357e4d0ad78e46bc7\/p\/uploads\/2020\/06\/e872532f.png\" alt=\"Right-click on &quot;Controllers&quot; to add a new controller with read\/write actions\" width=\"700\" height=\"285\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>The route is defined at the top. By default, this is set to <code>\/api\/controller<\/code>, where controller is the controller name (minus the \u201cController\u201d part). You can change this if you\u2019d like, but this is a good pattern for most people.<\/p>\n<pre>[Route(\"api\/[controller]\")]<\/pre>\n<p>You\u2019ll want to add a reference to your database service at the top of the class, and populate it in the constructor:<\/p>\n<pre>private readonly ExampleService _exampleService;&#13;\n&#13;\npublic ExampleController(ExampleService exampleService)&#13;\n{&#13;\n    _exampleService = exampleService;&#13;\n}<\/pre>\n<p>You\u2019ll want to change the HttpGet method to return an <code>ActionResult&lt;List&lt;T&gt;&gt;<\/code>\u00a0of the schema you\u2019re storing. (You will need to add a reference to your Models namespace, of course.) ActionResult is a dynamic type that wraps either the TValue inside of it, or an HTTP response result.<\/p>\n<pre>[HttpGet]&#13;\npublic ActionResult&lt;List&lt;User&gt;&gt; Get()&#13;\n{&#13;\n    return _exampleService.Get();&#13;\n}<\/pre>\n<p>If you want to add another Get method, but on a different route, you can pass it as a parameter in the attribute:<\/p>\n<pre>[HttpGet(\"otherroute\")]&#13;\npublic ActionResult&lt;List&lt;User&gt;&gt; GetSomeThingElse()&#13;\n{&#13;\n    return _exampleService.GetSomeThingElse();&#13;\n}<\/pre>\n<p>Then for the HttpPost method, call the Create method of your database service, and return\u00a0CreatedAtRoute, which will send a 201 response to the client informing them that the object creation was successful.<\/p>\n<pre>[HttpPost]&#13;\npublic ActionResult&lt;User&gt; Create(User userIn)&#13;\n{&#13;\n    _exampleService.Create(userIn);&#13;\n&#13;\n    return CreatedAtRoute(\"GetUser\", new { id = userIn.Id.ToString() }, userIn);&#13;\n}<\/pre>\n<p>For the other routes, you can configure them how you\u2019d like. The <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> idea is to handle the user input, perform any validations you need to, and call the proper database actions, returning the user a something useful or a status code telling them what happened.<\/p>\n<p>At this point though, you should be able to hit the IIS Express run button at the top, and test the API. This will default the route to be the weather services API that came with the boilerplate, so rather than typing it out every time, you can change the debug route under Project &gt; Properties &gt; Debug and changing the Launch Browser option.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-5194 size-full\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/21c577656ec5278cd08466f2e14d9ea1\/p\/uploads\/2020\/06\/237695be.png\" alt=\"change debug route\" width=\"700\" height=\"272\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Once you want to test on a remote server, you can create a production build from Build &gt; Publish Service. Select file output, and edit the configuration to suit your needs. For example, you can choose to build as self-contained, which will package all the .NET dependencies with the build, and also to publish as a single executable. You can change the target runtime here as well.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5195\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/c7711add5e41c1529e59e1dd60f7c062\/p\/uploads\/2020\/06\/0630d636.png\" alt=\"production build settings\" width=\"700\" height=\"350\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>If you want to publish to Linux, you\u2019ll need to add a runtime identifier to your <code>.csproj<\/code>\u00a0file.<\/p>\n<pre>&lt;RuntimeIdentifier&gt;linux-x64&lt;\/RuntimeIdentifier&gt;<\/pre>\n<p>Of course, the API featured here is unsecured. ASP.NET has a <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fsecurity%2Fauthentication%2F%3Fview%3Daspnetcore-3.1&amp;key=204a528a336ede4177fff0d84a044482\">great authentication system<\/a>, offering support for JWT based tokens, and can be configured to work with OAuth providers like <a rel=\"nofollow noopener noreferrer\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Faspnet%2Fcore%2Fsecurity%2Fauthentication%2Fsocial%2Fgoogle-logins%3Fview%3Daspnetcore-3.1&amp;key=204a528a336ede4177fff0d84a044482\">Google and Facebook<\/a>.\n<\/div>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener noreferrer\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/5126\/how-to-get-started-with-asp-net-for-api-development\/\" target=\"_blank\" rel=\"noopener noreferrer\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How to Get Started with ASP.NET for API Development \u2013 CloudSavvy IT&#8221; ASP.NET is a platform for building web APIs with C# and .NET. It\u2019s commonly used for app backends, and can automatically serialize classes to JSON. We\u2019ll walk through setting up a service that talks to a database and stores a list of objects&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":75938,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/06\/a448385b.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-75937","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/75937","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=75937"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/75937\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/75938"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=75937"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=75937"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=75937"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}