<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Aspnetcore - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Aspnetcore - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Sat, 30 May 2026 08:56:18 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/aspnetcore/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Set Semantic Version for .NET Core Apps and Libraries ]]>
                </title>
                <description>
                    <![CDATA[ Semantic Versioning (or SemVer for short) is a software versioning scheme that stipulates three-part version numbers of the form <major>.<minor>.<patch>, such as 1.0.2, with an optional prerelease suffix of the form -<prerelease>, as in 1.0.2-beta. S... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/set-semantic-versioning-for-net/</link>
                <guid isPermaLink="false">672df563eedb451674f63814</guid>
                
                    <category>
                        <![CDATA[ .NET ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Naveed Ausaf ]]>
                </dc:creator>
                <pubDate>Fri, 08 Nov 2024 11:26:27 +0000</pubDate>
                <media:content url="https://cdn.hashnode.com/res/hashnode/image/upload/v1731065367635/f8ce5091-d526-4d09-8282-2ffe63cead40.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p><a target="_blank" href="https://semver.org/">Semantic Versioning</a> (or SemVer for short) is a software versioning scheme that stipulates three-part version numbers of the form <code>&lt;major&gt;.&lt;minor&gt;.&lt;patch&gt;</code>, such as <code>1.0.2</code>, with an optional prerelease suffix of the form <code>-&lt;prerelease&gt;</code>, as in <code>1.0.2-beta</code>.</p>
<p>SemVer is perhaps the the most widely used versioning scheme today. For example, both <a target="_blank" href="https://learn.microsoft.com/en-us/nuget/concepts/package-versioning?tabs=semver20sort#pre-release-versions">Nuget</a> and <a target="_blank" href="https://docs.npmjs.com/about-semantic-versioning">npm</a> recommend and support it, and VS Code <a target="_blank" href="https://github.com/microsoft/vscode/releases">uses it</a> as well.</p>
<p>In most GitHub repos that use the GitHub Releases feature to publish releases, you would see a SemVer version number in the latest release badge on the home page, as can be seen in the screenshot below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1730988665455/34706cc9-7cf3-401c-9407-2f15933fef49.png" alt="Latest release badge in Next.js GitHub repository showing the three-part Semantic Versioning version number 15.0.3" class="image--center mx-auto" width="411" height="206" loading="lazy"></p>
<p>I frequently need to set a SemVer version number when building ASP.NET Core API projects, and then read or report this at runtime.</p>
<p>For example, if I build a minimal API with its version set to <code>1.0.2-beta</code>, this would be reported by a <code>/version</code> endpoint exposed by the API, as shown in the screenshot below from <a target="_blank" href="https://hoppscotch.io/">Hoppscotch</a> (this is a <a target="_blank" href="https://www.postman.com/">Postman</a>-like tool with the convenience that it runs the browser):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1730746046707/eb8968ef-41c7-4919-a0ed-7aeb25e0a03d.png" alt="Screenshot of Hoppscotch shows that a was call made to the /version endpoint of an API running locally (on localhost). The result was a JSON document containing a &quot;version&quot; property whose value is the API's SemVer version number of &quot;1.0.2-beta&quot;." class="image--center mx-auto" width="623" height="488" loading="lazy"></p>
<p>Checking that the version reported from deployed services, such as web apps and APIs, is correct is a crucial part of my CD pipeline and is one of the smoke tests I use to determine if a deployment succeeded.</p>
<p>One slight complication when setting a SemVer version number on .NET assemblies is that .NET originally used four part version numbers like <code>1.0.3.212</code> and assemblies still have these (assembly is the .NET term for <a target="_blank" href="https://learn.microsoft.com/en-us/dotnet/standard/assembly/">units of code compiled to .NET bytecode</a>, the most typical of these being dll’s and exe’s).</p>
<p>The other is that .NET has not one but many, slightly different, version numbers that are present in the same assembly.</p>
<p>In this article, I’ll show you how to sidestep these quirks and stamp a SemVer version number on a .NET assembly during build, then read it back at runtime.</p>
<h2 id="heading-table-of-contents">Table of Contents</h2>
<ul>
<li><p><a class="post-section-overview" href="#heading-structure-of-a-semver-version-number">Structure of a SemVer Version Number</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-the-many-version-numbers-of-a-net-assembly">The Many Version Numbers of a .NET Assembly</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-set-a-semver-version-number">How to Set a SemVer Version Number</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-how-to-read-an-assemblys-semver-version-at-runtime">How to Read an Assembly’s SemVer Version at Runtime</a></p>
</li>
<li><p><a class="post-section-overview" href="#heading-conclusion">Conclusion</a></p>
</li>
</ul>
<h2 id="heading-structure-of-a-semver-version-number">Structure of a SemVer Version Number</h2>
<p>Consider a SemVer version number like <code>1.0.2</code> or <code>1.0.2-beta</code>. It has the form <code>&lt;major&gt;</code>.<code>&lt;minor&gt;</code>.<code>&lt;patch&gt;</code>-<code>&lt;prerelease&gt;</code></p>
<p>This is what the various components mean:</p>
<p>The <code>&lt;major&gt;</code> component of the version number would be incremented only if the new release would break an existing (most recent) release.</p>
<p>In case of a UI app, clients may be taken to mean <em>human clients</em>. So if the new release would break users’ existing assets such as workflow definitions, this would call for incrementing the major version number. In this event, if the previous release was <code>1.0.2</code>, the new release should be <code>2.0.0</code> (all lower components of the version number would reset).</p>
<p>In case of a library, such as a library package on Nuget or NPM, the clients would be other code. So if the new release would break existing client code, i.e. it would not be backwards compatible with its own previous version, then again it is the <code>&lt;major&gt;</code> component would be incremented.</p>
<p><code>&lt;minor&gt;</code> is incremented if new features have been added but the new version is still backwards compatible. So from <code>1.0.2</code> you would go to <code>1.1.0</code>.</p>
<p><code>&lt;patch&gt;</code> is incremented when a new release needs to be made even though there is no breaking change and no new functionality has been added. This could happen, for example, if there was a bugfix that had to be released.</p>
<p><code>-&lt;prerelease&gt;</code> suffix is optional. It is typically suffixed to a three part version number when software needs to be made available during prerelease testing phases such as alpha and beta. For example, before generally releasing version <code>1.0.2</code> of your software, you can make it available to your beta testers as <code>1.0.2-beta</code>.</p>
<p>The <code>&lt;prerelease&gt;</code> component can pretty much be any string of your choosing and the only requirement is that it is either an <em>alphanumeric identifier</em> such as <code>beta</code> or <code>12</code> or <code>alpha2</code> (no characters other than numbers or letters of the alphabet) or multiple alphanumeric identifiers separated by a dot(<code>.</code>) e.g. <code>development.version</code>.</p>
<h2 id="heading-the-many-version-numbers-of-a-net-assembly">The Many Version Numbers of a .NET Assembly</h2>
<p>As Andrew Lock’s <a target="_blank" href="https://andrewlock.net/version-vs-versionsuffix-vs-packageversion-what-do-they-all-mean/#how-to-set-the-version-number-when-you-build-your-app-library">article on .NET versioning</a> explains, a .NET assembly has not one but several different version numbers:</p>
<ul>
<li><p><strong>AssemblyVersion:</strong> This is a four part version number, for example, <code>1.0.2.0</code>. It is used by the runtime when loading linked assemblies.</p>
</li>
<li><p><strong>FileVersion:</strong> This is the version number reported for a <strong>.dll</strong> file in Windows File Explorer when you right click the assembly and select Properties.</p>
</li>
<li><p><strong>InformationalVersion:</strong> Yet another version number and, like FileVersion, can be seen in Properties dialog if you right-click the assembly in Windows and select Properties. This can contain strings and not only integers and dots that AssemblyVersion and FileVersion are constrained to.</p>
</li>
<li><p><strong>PackageVersion:</strong> If the project is a Nuget package, this would be the version number of the package that the assembly is part of.</p>
</li>
</ul>
<p>All of these version numbers are emitted into the assembly during compilation as metadata. You can see them if you inspect the assembly with <a target="_blank" href="https://www.jetbrains.com/decompiler/">JetBrains dotPeek</a> (free) or <a target="_blank" href="https://www.red-gate.com/products/reflector/">Red gate Reflector</a> (not free) or similar.</p>
<p>FileVersion and InformationalVersion can also be seen in the Details tab of the Properties dialog that appears when you right-click the assembly file in Windows File Explorer and select Properties:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1730755185100/d444a84b-5148-47e6-ab75-951d9f0f73ac.png" alt="Properties Dialog for a compiled .NET DLL in Windows File Explorer. It shows the DLL's &quot;File Version&quot; attribute with value &quot;1.0.2.0&quot; and its &quot;Product version&quot; attribute with value &quot;1.0.2-beta&quot;" class="image--center mx-auto" width="480" height="621" loading="lazy"></p>
<p>In the screenshot above, “Product version” is the caption for InformationalVersion whereas “File version” is the caption for FIleVersion.</p>
<p>Of the four types of version numbers described above, only the first three apply to any assembly (i.e. whether or not the assembly is part of a Nuget package).</p>
<p>Of those three, AssemblyVersion alsways adds a <code>0</code> in the fourth place if you try to set a SemVer version which only has three numbers (plus an optional <em>prerelease</em> suffix). For example if you try to set a SemVer version of <code>1.0.2-beta</code> during build and then read the AssemblyVersion value at run time in the assembly, it would be <code>1.0.2.0</code>.</p>
<p>FileVersion does the same, as shown in the screenshot above.</p>
<p>InformationalVersion is the only version number which would get set exactly to the server version you set during build, as the screenshot above shows.</p>
<p>Therefore, InformationalVersion is what we need to set with a SemVer version during build, and read back at run time.</p>
<h2 id="heading-how-to-set-a-semver-version-number">How to Set a SemVer Version Number</h2>
<p>There are two things you need to do to set a SemVer version number on an assembly during build.</p>
<p><strong>First,</strong> in a <code>&lt;PropertyGroup&gt;</code> element in the project’s <code>csproj</code> file, add element <code>&lt;IncludeSourceRevisionInInformationalVersion&gt;false&lt;/IncludeSourceRevisionInInformationalVersion&gt;</code>:</p>
<pre><code class="lang-xml"><span class="hljs-tag">&lt;<span class="hljs-name">PropertyGroup</span>&gt;</span>
 ...
 <span class="hljs-tag">&lt;<span class="hljs-name">IncludeSourceRevisionInInformationalVersion</span>&gt;</span>false<span class="hljs-tag">&lt;/<span class="hljs-name">IncludeSourceRevisionInInformationalVersion</span>&gt;</span> 
<span class="hljs-tag">&lt;/<span class="hljs-name">PropertyGroup</span>&gt;</span>
</code></pre>
<p>As described in <a target="_blank" href="https://developercommunity.visualstudio.com/t/Build-adds-string-to-assembly-Informatio/10515014?sort=newest">this issue</a>, this ensures that InformationalVersion is set exactly to the SemVer version number we specified and does not get a <code>+&lt;hash code&gt;</code> tacked on at the end.</p>
<p><strong>Second,</strong> pass the version number as value of <code>Version</code> property passed to <code>dotnet build</code> command e.g.:</p>
<pre><code class="lang-yaml"><span class="hljs-string">dotnet</span> <span class="hljs-string">build</span> <span class="hljs-string">--configuration</span> <span class="hljs-string">Release</span> <span class="hljs-string">-p</span> <span class="hljs-string">Version=1.0.2-beta</span>
</code></pre>
<p>This would set InformationalVersion in the compiled assembly (.exe or .dll file) to <code>1.0.2-beta</code>.</p>
<p>Incidentally, it would also set AssemblyVersion and FileVersion (an extra <code>0</code> would be added to the end of <code>1.0.2</code>) but we are not interested in those.</p>
<p>Note that instead pf passing <code>Version</code> argument on the command line, you can set MS Build property <code>&lt;Version&gt;1.0.2-beta&lt;/Version&gt;</code> in a <code>&lt;PropertyGroup&gt;</code> element in the csproj file. However passing a value of <code>Version</code> parameter to <code>dotnet build</code> is simpler because the csproj file does not need to be modified everytime the version number is incremented. This is helpful in CD pipelines. If you do set <code>&lt;Version&gt;</code> in csproj file, this will be overridden by whatever you provide on the <code>dotnet build</code> command line as the value of the <code>Version</code> property.</p>
<h2 id="heading-how-to-read-an-assemblys-semver-version-at-runtime">How to Read an Assembly’s SemVer Version at Runtime</h2>
<p>Code that reads InfromationalVersion at run time is as follows:</p>
<pre><code class="lang-csharp"><span class="hljs-keyword">string</span>? version = Assembly.GetEntryAssembly()?.
  GetCustomAttribute&lt;AssemblyInformationalVersionAttribute&gt;()?.
  InformationalVersion;
</code></pre>
<p>In my minimal APIs, to add a <code>/version</code> endpoint as I showed in the Introduction section above, I place the above snippet in <code>Program.cs</code>, then add the following snippet immediately after. Note that the whole thing should appear <strong>before</strong> <code>builder.Build()</code> <strong>is called</strong>:</p>
<pre><code class="lang-csharp"><span class="hljs-comment">//this object of an anonymous type will </span>
<span class="hljs-comment">//be serialised as JSON in response body</span>
<span class="hljs-comment">//when returned by a handler</span>
<span class="hljs-keyword">var</span> objVersion = <span class="hljs-keyword">new</span> { Version = version ?? <span class="hljs-string">""</span> };

<span class="hljs-comment">//OTHER CODE</span>
<span class="hljs-comment">//var app = builder.Build()</span>
</code></pre>
<p>After <code>builder.Build()</code> is called, I create the handler for the <code>/version</code> endpoint:</p>
<pre><code class="lang-csharp">app.MapGet(<span class="hljs-string">"/version"</span>, () =&gt; objVersion);
</code></pre>
<p>Now when I run the API project and call the <code>/version</code> endpoint, I get the version number back in a JSON object in HTTP response body:</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.2-beta"</span>
}
</code></pre>
<p>This is what the Hoppscotch screenshot in the Introduction showed.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This article showed you how to set a SemVer version number in your .NET assemblies.</p>
<p>It also showed you how to read the version number at runtime.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Master ASP.NET Core by Building Three Projects ]]>
                </title>
                <description>
                    <![CDATA[ Are you ready to enhance your web development skills with ASP.NET Core MVC?  We just posted a course on the freeCodeCamp.org YouTube channel that's designed to take you from a novice to a skilled ASP.NET Core developer. Alen Omeri created this course... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/master-asp-net-core-by-building-three-projects/</link>
                <guid isPermaLink="false">66b2059f125aeccef6f65cf6</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Wed, 20 Mar 2024 16:40:40 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/03/aspnet.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Are you ready to enhance your web development skills with ASP.NET Core MVC? </p>
<p>We just posted a course on the freeCodeCamp.org YouTube channel that's designed to take you from a novice to a skilled ASP.NET Core developer. Alen Omeri created this course.</p>
<h2 id="heading-what-you-will-learn">What You Will Learn</h2>
<p>This course is structured to provide a step-by-step learning experience, starting with a basic full-stack project and progressing to more complex applications. Here is what the course covers:</p>
<p><strong>Getting Started with ASP.NET Core MVC</strong></p>
<p>Start by understanding the fundamental structure of ASP.NET Core MVC. This introductory segment lays the foundation for your journey ahead.</p>
<p><strong>Project 1: Food Menu Web App</strong>:</p>
<ul>
<li><strong>Setup</strong>: Learn to initialize your project and get ready for development.</li>
<li><strong>Modeling and Database Integration</strong>: Dive into creating models, setting up the context, and connecting your project to a database.</li>
<li><strong>Controller Logic</strong>: Develop the controller and its methods to manage data flow between the database and your views.</li>
<li><strong>Enhancements</strong>: Add multiple data entries to the database manually, implement a search bar, and refine the user interface.</li>
</ul>
<p><strong>Project 2: Google Docs Clone</strong>:</p>
<ul>
<li><strong>Introduction and Setup</strong>: Get acquainted with the project and set up your development environment.</li>
<li><strong>Core Development</strong>: Create models, set up the database, develop a controller, and handle page creation.</li>
<li><strong>Advanced Features</strong>: Incorporate the TinyMCE library, manage document editing, and format the delete page. Learn how to convert documents to PDF for a comprehensive feature set.</li>
</ul>
<p><strong>Project 3: Stripe Web App</strong>:</p>
<ul>
<li><strong>Introduction to Stripe</strong>: Understand the essentials of integrating Stripe into your web application.</li>
<li><strong>Project Initialization</strong>: Set up your project in Visual Studio and prepare for development.</li>
<li><strong>Building the Application</strong>: Create models, set up the database, store products, and display them. Dive into the technicalities of creating a checkout method and test your application rigorously.</li>
</ul>
<h2 id="heading-why-this-course">Why This Course?</h2>
<p>This video course on freeCodeCamp.org's YouTube channel is more than just a tutorial; it's a hands-on learning experience that emphasizes real-world application. By building three distinct projects, you'll gain a thorough understanding of ASP.NET Core MVC, enabling you to develop your own web applications confidently.</p>
<p>Watch the full course on the <a target="_blank" href="https://youtu.be/6SAFgcMie4U">freeCodeCamp.org YouTube channel</a> (2.5 hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/6SAFgcMie4U" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn ASP.NET Core by Building an Auction Application ]]>
                </title>
                <description>
                    <![CDATA[ ASP.NET course can be used to create robust, scalable, and dynamic applications. We just posted a course on the freeCodeCamp.org YouTube channel that is an opportunity for learners to dive deep into the world of fullstack development using ASP.NET Co... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-asp-net-core-by-building-an-auction-application/</link>
                <guid isPermaLink="false">66b203f6a8b92c9329236482</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Thu, 08 Feb 2024 16:22:49 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2024/02/aspnet.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>ASP.NET course can be used to create robust, scalable, and dynamic applications.</p>
<p>We just posted a course on the freeCodeCamp.org YouTube channel that is an opportunity for learners to dive deep into the world of fullstack development using ASP.NET Core. Alen Omeri designed the course to guide you through the process of building a complete auction web application from scratch.</p>
<p>ASP.NET Core is an open-source, high-performance framework developed by Microsoft for building modern, cloud-based, and internet-connected applications. It offers a versatile platform for developing web apps, IoT apps, mobile backends, and more, leveraging the .NET ecosystem. Its performance, scalability, and support for cross-platform development make it an attractive choice for developers.</p>
<h2 id="heading-course-overview">Course Overview</h2>
<p>The course kickstarts with an overview, setting the stage for what learners can expect. It outlines the project scope, the core functionalities of the auction web application, and an introduction to the ASP.NET Core framework and its advantages. Here are the other sections of the course:</p>
<h3 id="heading-models-amp-database-setup">Models &amp; Database Setup</h3>
<p>The journey begins with designing the application's backbone—defining models and setting up the database. This section covers Entity Framework Core, a powerful ORM (Object-Relational Mapping) framework for .NET, which simplifies data access by allowing developers to work with a database using .NET objects.</p>
<h3 id="heading-index-page">Index Page</h3>
<p>Next, the course walks you through developing the Index Page, where all the auction items are displayed. This involves integrating models with views and controllers, showcasing the MVC (Model-View-Controller) architecture that ASP.NET Core champions for organizing application logic.</p>
<h3 id="heading-create-page-amp-file-uploads">Create Page &amp; File Uploads</h3>
<p>Building upon the basics, this section dives into creating new auction listings. It covers handling form data, implementing file uploads, and ensuring data validation, providing a comprehensive guide on managing user inputs and assets.</p>
<h3 id="heading-details-page">Details Page</h3>
<p>Focusing on individual auction items, the Details Page section teaches you to display item specifics. This includes integrating dynamic data into views and enhancing user experience by presenting detailed information about each listing.</p>
<h3 id="heading-css-structure-search-bar-amp-pagination">CSS + Structure, Search Bar &amp; Pagination</h3>
<p>Aesthetic and functionality enhancements come next. This part of the course emphasizes CSS for styling, structuring your application for a cleaner look, and implementing features like a search bar and pagination for improved navigation and usability.</p>
<h3 id="heading-adding-bids-amp-closing-the-bidding">Adding Bids &amp; Closing the Bidding</h3>
<p>A crucial functionality of any auction application, this section covers how to add bidding features to your application, manage bid submissions, and automate the closing of bids, ensuring a real-time and interactive bidding process.</p>
<h3 id="heading-adding-comments">Adding Comments</h3>
<p>Engagement and interaction are further elevated by incorporating a commenting feature. This segment teaches the implementation of a comments section for each auction item, facilitating user discussions and feedback.</p>
<h3 id="heading-my-listings-amp-my-bids">My Listings &amp; My Bids</h3>
<p>Personalization features such as "My Listings" and "My Bids" pages allow users to track their activity on the platform. These sections delve into creating user-specific views that display the listings a user has posted and the bids they've made.</p>
<h3 id="heading-project-recap">Project Recap</h3>
<p>The course concludes with a comprehensive recap, revisiting the key technologies and concepts covered throughout the course. It ensures a solid understanding of the project and the ASP.NET Core framework, empowering learners to embark on their fullstack development journey with confidence.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Alen Omeri's expertise in ASP.NET Core and clear instructional style make complex topics accessible to learners of all levels. The course's hands-on approach, with a real-world project, ensures that by the end, you have not just theoretical knowledge but practical experience in fullstack development with ASP.NET Core.</p>
<p>Watch the full course on <a target="_blank" href="https://youtu.be/nKovSmd5DWY">the freeCodeCamp.org YouTube channel</a> (2-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/nKovSmd5DWY" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn ASP.NET Core MVC (.NET 6) ]]>
                </title>
                <description>
                    <![CDATA[ ASP.NET Core MVC is a rich framework created by Microsoft for building web apps and APIs using the Model-View-Controller design pattern. We just published a course on the freeCodeCamp.org YouTube channel that will teach you how to use the latest vers... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/learn-asp-net-core-mvc-net-6/</link>
                <guid isPermaLink="false">66b203f882069b4c678c98ef</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Mon, 22 Nov 2021 18:49:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/11/maxresdefault.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>ASP.NET Core MVC is a rich framework created by Microsoft for building web apps and APIs using the Model-View-Controller design pattern.</p>
<p>We just published a course on the freeCodeCamp.org YouTube channel that will teach you how to use the latest version of ASP.NET Core MVC.</p>
<p>Bhrugen Patel developed this course. Bhrugen is an experienced developer who has been teaching about ASP.NET for years.</p>
<p>In this course you will learn the basics of .NET Core (.NET 6) and then build a simple CRUD application with MVC. The course is for anyone who is familiar with ASP.NET basics and wants to know how to architect and code real-world applications in ASP.NET Core.</p>
<p>Here are the different parts of this course:</p>
<h3 id="heading-section-1-introduction">Section 1: Introduction</h3>
<ul>
<li>Welcome</li>
<li>Live Preview</li>
<li>Tools Needed </li>
<li>Introduction to .Net Core</li>
<li>Dependency injection</li>
<li>Create Project</li>
<li>Project File</li>
<li>Launch settings</li>
<li>Programs</li>
<li>MVC Architecture</li>
<li>Routing Overview</li>
<li>Routing in Action</li>
<li>Default Views</li>
<li>Tag Helper</li>
<li>Action Result</li>
<li>Hot Reload</li>
</ul>
<h3 id="heading-section-2-database-setup">Section 2: Database Setup</h3>
<ul>
<li>Create Category Model</li>
<li>Add Data Annotations</li>
<li>Add Connection String</li>
<li>Add ApplicationDbContext</li>
<li>Setup Program.cs to use DbContext</li>
<li>Check Database</li>
</ul>
<h3 id="heading-section-3-category-crud-operations">Section 3: Category CRUD Operations</h3>
<ul>
<li>Create Category Controller</li>
<li>Retrieve all Categories </li>
<li>Display all Categories</li>
<li>Bootswatch Theme</li>
<li>Bootstrap Icons</li>
<li>Create Category View</li>
<li>Demo- Create Category </li>
<li>Server Side Validation</li>
<li>Custom Validation</li>
<li>Add Client Side Validation</li>
<li>Display Name and Range Validation</li>
<li>Edit Category - GET</li>
<li>Edit Category - POST</li>
<li>Delete Category </li>
<li>Tempdata</li>
<li>Partial views</li>
<li>Toastr Alerts </li>
<li>Scaffold CRUD</li>
<li>Create SQL Database on Azure</li>
<li>Azure Deployment</li>
<li>Congratulations</li>
</ul>
<p>Watch the full course below or <a target="_blank" href="https://youtu.be/hZ1DASYd9rk">on the freeCodeCamp.org YouTube channel</a> (3-hour watch).</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/hZ1DASYd9rk" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Create a C# App in One Hour using ASP.NET ]]>
                </title>
                <description>
                    <![CDATA[ C# and ASP.NET are used as the server-side programming language on many websites. We've released a course on the freeCodeCamp.org YouTube channel that will teach you how to create a web database app in ASP.NET Core using Visual Studio and the C# lang... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/asp-net-crash-course/</link>
                <guid isPermaLink="false">66b20098712508eb160677fa</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Thu, 19 Nov 2020 19:12:04 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/11/aspnet.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>C# and ASP.NET are used as the server-side programming language on many websites.</p>
<p>We've released a course on the freeCodeCamp.org YouTube channel that will teach you how to create a web database app in ASP.NET Core using Visual Studio and the C# language.</p>
<p>The course was created by Shad Sluiter. Shad is a professor of computer science at Grand Canyon University. Over the past 20 years he has taught students from grade 6 to senior citizens in many aspects of technology.</p>
<p>The web app you will create is a basic CRUD app that interacts with a database. </p>
<p>This tutorial will show you how to:</p>
<ul>
<li>Use the MVC (Model View Controller) design pattern.</li>
<li>Configure database tables using the Entity framework.</li>
<li>Setup classes as models. </li>
<li>Customize Razor forms.</li>
<li>Create methods inside a controller.</li>
<li>Style an ASP.NET page with CSS.</li>
<li>Use Bootstrap classes.</li>
<li>Create a search function to filter database results.</li>
</ul>
<p>Watch the full course on <a target="_blank" href="https://youtu.be/BfEjDD8mWYg">the freeCodeCamp.org YouTube channel</a> (1-hour watch).</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create a Multi-Language Translator Using Blazor and Azure Cognitive Services ]]>
                </title>
                <description>
                    <![CDATA[ By Ankit Sharma Introduction In this article, we will create a multilanguage translator using Blazor and the Translate Text Azure Cognitive Service.  This translator will be able to translate between all the languages supported by the Translate Text ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-a-multi-language-translator-using-blazor-and-azure-cognitive-services/</link>
                <guid isPermaLink="false">66d45da23dce891ac3a967ae</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Azure ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Blazor ]]>
                    </category>
                
                    <category>
                        <![CDATA[ translation ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Mon, 09 Mar 2020 14:14:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9c3b740569d1a4ca30de.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ankit Sharma</p>
<h2 id="heading-introduction">Introduction</h2>
<p>In this article, we will create a multilanguage translator using Blazor and the Translate Text Azure Cognitive Service. </p>
<p>This translator will be able to translate between all the languages supported by the Translate Text API. Currently, the Translate Text API supports more than 60 languages for translation. </p>
<p>The application will accept the text to translate and the target language as the input and returns the translated text and the detected language for the input text as the output.</p>
<p>Take a look at the output shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorTranslator.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li>Install the latest .NET Core 3.1 SDK from <a target="_blank" href="https://dotnet.microsoft.com/download/dotnet-core/3.1">https://dotnet.microsoft.com/download/dotnet-core/3.1</a></li>
<li>Install the latest version of Visual Studio 2019 from <a target="_blank" href="https://visualstudio.microsoft.com/downloads/">https://visualstudio.microsoft.com/downloads/</a></li>
<li>An Azure subscription account. You can create a free Azure account at <a target="_blank" href="https://azure.microsoft.com/en-in/free/">https://azure.microsoft.com/en-in/free/</a></li>
</ul>
<h2 id="heading-source-code">Source Code</h2>
<p>You can get the source code from <a target="_blank" href="https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services">GitHub</a>.</p>
<h2 id="heading-create-the-azure-translator-text-cognitive-services-resource">Create the Azure Translator Text Cognitive Services resource</h2>
<p>Log in to the Azure portal and search for the cognitive services in the search bar and click on the result. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/CreateTextCogServ-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On the next screen, click on the <code>Add</code> button. It will open the cognitive services marketplace page. Search for the <code>Translator Text</code> in the search bar and click on the search result. It will open the Translator Text API page. Click on the <code>Create</code> button to create a new Translator Text resource. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/SelectTranslatorTextCogServ.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On the <code>Create</code> page, fill in the details as indicated below.</p>
<ul>
<li>Name: Give a unique name for your resource.</li>
<li>Subscription: Select the subscription type from the dropdown.</li>
<li>Pricing tier: Select the pricing tier as per your choice.</li>
<li>Resource group: Select an existing resource group or create a new one.</li>
</ul>
<p>Click on the <code>Create</code> button. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/ConfigureTranslatorTextCogServ.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After your resource is successfully deployed, click on the “Go to resource” button. You can see the Key and the endpoint for the newly created Translator Text resource. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/TextCogServKey.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Make a note of the key, as we will be using this in the latter part of this article to request the translations from the Translator Text API. The values are masked here for privacy.</p>
<h2 id="heading-create-a-server-side-blazor-application">Create a Server-Side Blazor Application</h2>
<p>Open Visual Studio 2019 and click on “Create a new project”. Select “Blazor App” and click on the “Next” button. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/CreateProject-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On the next window, put the project name as <code>BlazorTranslator</code> and click on the “Create” button. The next window will ask you to select the type of Blazor app. Select <code>Blazor Server App</code> and click on the Create button to create a new server-side Blazor application. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorTemplate-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-creating-the-models">Creating the Models</h2>
<p>Right-click on <code>BlazorTranslator</code> project and select Add &gt;&gt; New Folder. Name the folder as Models. Again, right-click on the Models folder and select Add &gt;&gt; Class to add a new class file. Put the name of your class as <code>LanguageDetails.cs</code> and click Add.</p>
<p>Open <code>[LanguageDetails.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Models/LanguageDetails.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">LanguageDetails</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> NativeName { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Dir { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Similarly, add a new class file <code>[TextResult.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Models/TextResult.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">TextResult</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Text { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Script { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Add a new class file <code>[Translation.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Models/Translation.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Translation</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Text { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> TextResult Transliteration { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> To { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Create a class file <code>[DetectedLanguage.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Models/DetectedLanguage.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">DetectedLanguage</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Language { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">float</span> Score { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Create a class file <code>[TranslationResult.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Models/TranslationResult.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">TranslationResult</span>
    {
        <span class="hljs-keyword">public</span> DetectedLanguage DetectedLanguage { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> TextResult SourceText { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> Translation[] Translations { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Finally, create the class file <code>[AvailableLanguage.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Models/AvailableLanguage.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">using</span> System.Collections.Generic;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">AvailableLanguage</span>
    {
      <span class="hljs-keyword">public</span> Dictionary&lt;<span class="hljs-keyword">string</span>, LanguageDetails&gt; Translation { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<h2 id="heading-create-the-translation-service">Create the Translation service</h2>
<p>Right-click on the <code>BlazorTranslator/Data</code> folder and select Add &gt;&gt; Class to add a new class file. Put the name of the file as <code>TranslationService.cs</code> and click on Add. Open <code>[TranslationService.cs](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Data/TranslationService.cs)</code> file and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">using</span> BlazorTranslator.Models;
<span class="hljs-keyword">using</span> Newtonsoft.Json;
<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Net.Http;
<span class="hljs-keyword">using</span> System.Text;
<span class="hljs-keyword">using</span> System.Threading.Tasks;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorTranslator.Data</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">TranslationService</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task&lt;TranslationResult[]&gt; GetTranslatation(<span class="hljs-keyword">string</span> textToTranslate, <span class="hljs-keyword">string</span> targetLanguage)
        {
            <span class="hljs-keyword">string</span> subscriptionKey = <span class="hljs-string">"af19d996a3cb4a70a808567aad5bc41a"</span>;
            <span class="hljs-keyword">string</span> apiEndpoint = <span class="hljs-string">"https://api.cognitive.microsofttranslator.com/"</span>;
            <span class="hljs-keyword">string</span> route = <span class="hljs-string">$"/translate?api-version=3.0&amp;to=<span class="hljs-subst">{targetLanguage}</span>"</span>;
            <span class="hljs-keyword">string</span> requestUri = apiEndpoint + route;
            TranslationResult[] translationResult = <span class="hljs-keyword">await</span> TranslateText(subscriptionKey, requestUri, textToTranslate);
            <span class="hljs-keyword">return</span> translationResult;
        }

        <span class="hljs-keyword">async</span> Task&lt;TranslationResult[]&gt; TranslateText(<span class="hljs-keyword">string</span> subscriptionKey, <span class="hljs-keyword">string</span> requestUri, <span class="hljs-keyword">string</span> inputText)
        {
            <span class="hljs-keyword">object</span>[] body = <span class="hljs-keyword">new</span> <span class="hljs-keyword">object</span>[] { <span class="hljs-keyword">new</span> { Text = inputText } };
            <span class="hljs-keyword">var</span> requestBody = JsonConvert.SerializeObject(body);

            <span class="hljs-keyword">using</span> (<span class="hljs-keyword">var</span> client = <span class="hljs-keyword">new</span> HttpClient())
            <span class="hljs-keyword">using</span> (<span class="hljs-keyword">var</span> request = <span class="hljs-keyword">new</span> HttpRequestMessage())
            {
                request.Method = HttpMethod.Post;
                request.RequestUri = <span class="hljs-keyword">new</span> Uri(requestUri);
                request.Content = <span class="hljs-keyword">new</span> StringContent(requestBody, Encoding.UTF8, <span class="hljs-string">"application/json"</span>);
                request.Headers.Add(<span class="hljs-string">"Ocp-Apim-Subscription-Key"</span>, subscriptionKey);

                HttpResponseMessage response = <span class="hljs-keyword">await</span> client.SendAsync(request).ConfigureAwait(<span class="hljs-literal">false</span>);
                <span class="hljs-keyword">string</span> result = <span class="hljs-keyword">await</span> response.Content.ReadAsStringAsync();
                TranslationResult[] deserializedOutput = JsonConvert.DeserializeObject&lt;TranslationResult[]&gt;(result);

                <span class="hljs-keyword">return</span> deserializedOutput;
            }
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task&lt;AvailableLanguage&gt; <span class="hljs-title">GetAvailableLanguages</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">string</span> endpoint = <span class="hljs-string">"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&amp;scope=translation"</span>;
            <span class="hljs-keyword">var</span> client = <span class="hljs-keyword">new</span> HttpClient();
            <span class="hljs-keyword">using</span> (<span class="hljs-keyword">var</span> request = <span class="hljs-keyword">new</span> HttpRequestMessage())
            {
                request.Method = HttpMethod.Get;
                request.RequestUri = <span class="hljs-keyword">new</span> Uri(endpoint);
                <span class="hljs-keyword">var</span> response = <span class="hljs-keyword">await</span> client.SendAsync(request).ConfigureAwait(<span class="hljs-literal">false</span>);
                <span class="hljs-keyword">string</span> result = <span class="hljs-keyword">await</span> response.Content.ReadAsStringAsync();

                AvailableLanguage deserializedOutput = JsonConvert.DeserializeObject&lt;AvailableLanguage&gt;(result);

                <span class="hljs-keyword">return</span> deserializedOutput;
            }
        }
    }
}
</code></pre>
<p>We have defined a <code>GetTranslatation</code> method which will accept two parameters – the text to translate and the target language. We will set the subscription key for the Azure Translator Text cognitive service and define a variable for the global endpoint for Translator Text. The request URL contains the API endpoint along with the target language.</p>
<p>Inside the <code>TranslateText</code> method, we will create a new <code>HttpRequestMessage</code>. This HTTP request is a Post request. We will pass the subscription key in the header of the request. The Translator Text API returns a JSON object, which will be deserialized to an array of type <code>TranslationResult</code>. The output contains the translated text as well as the language detected for the input text.</p>
<p>The <code>GetAvailableLanguages</code> method will return the list of all the language supported by the Translate Text API. We will set the request URI and create a <code>HttpRequestMessage</code> which will be a Get request. This request URL will return a JSON object which will be deserialized to an object of type <code>AvailableLanguage</code>.</p>
<h2 id="heading-configuring-the-service">Configuring the Service</h2>
<p>To make the service available to the components we need to configure it on the server-side app. Open the <code>Startup.cs</code> file. Add the following line inside the <code>[ConfigureServices](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Startup.cs#L28)</code> method of Startup class.</p>
<pre><code>services.AddSingleton&lt;TranslationService&gt;();
</code></pre><h2 id="heading-creating-the-blazor-ui-component">Creating the Blazor UI Component</h2>
<p>We will add the Razor page in <code>BlazorTranslator/Pages</code> folder. By default, we have “Counter” and “Fetch Data” pages provided in our application. These default pages will not affect our application but for the sake of this tutorial, we will delete fetchdata and counter pages from <code>BlazorTranslator/Pages</code> folder.</p>
<p>Right-click on <code>BlazorTranslator/Pages</code> folder and then select Add &gt;&gt; New Item. An “Add New Item” dialog box will open, select “Visual C#” from the left panel, then select “Razor Component” from the templates panel, put the name as <code>Translator.razor</code>. Click <code>Add</code>. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/AddRazorComp-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Open the <code>[Translator.razor](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Pages/Translator.razor)</code> file and add the following code at the top.</p>
<pre><code>@page <span class="hljs-string">"/translator"</span>
@using BlazorTranslator.Models
@using BlazorTranslator.Data
@inject TranslationService translationService
</code></pre><p>We have defined the route for this component. We are also injecting the <code>TranslationService</code> in this component.</p>
<p>Now we will add the following HTML code in this file.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Multilanguage translator using Microsoft Translator API Cognitive Service<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"col-md-6"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> @<span class="hljs-attr">bind</span>=<span class="hljs-string">"inputLanguage"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">""</span>&gt;</span>-- Select input language --<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                @foreach (KeyValuePair<span class="hljs-tag">&lt;<span class="hljs-name">string,</span> <span class="hljs-attr">LanguageDetails</span>&gt;</span> lang in LanguageList)
                {
                    <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"@lang.Key"</span>&gt;</span>@lang.Value.Name<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter text to translate"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control translation-box"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"5"</span> @<span class="hljs-attr">bind</span>=<span class="hljs-string">"@inputText"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"col-md-6"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> @<span class="hljs-attr">onchange</span>=<span class="hljs-string">"SelectLanguage"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">""</span>&gt;</span>-- Select target language --<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                @foreach (KeyValuePair<span class="hljs-tag">&lt;<span class="hljs-name">string,</span> <span class="hljs-attr">LanguageDetails</span>&gt;</span> lang in LanguageList)
                {
                    <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"@lang.Key"</span>&gt;</span>@lang.Value.Name<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">disabled</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control translation-box"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"5"</span>&gt;</span>@outputText<span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text-center"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary btn-lg"</span> @<span class="hljs-attr">onclick</span>=<span class="hljs-string">"Translate"</span>&gt;</span>Translate<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We have defined two dropdown lists, one each for input language and the target language. The Azure Translate Text API will detect the input language and we will use that value to populate the dropdown for input language. We have also defined two text areas for the input and the translated text.</p>
<p>Finally, add the following code in the <code>@code</code> section of the page.</p>
<pre><code class="lang-c#">@code {
    <span class="hljs-keyword">private</span> TranslationResult[] translations;
    <span class="hljs-keyword">private</span> AvailableLanguage availableLanguages;

    <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> outputLanguage { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">private</span> <span class="hljs-keyword">string</span> inputLanguage { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }

    <span class="hljs-keyword">string</span> inputText { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    <span class="hljs-keyword">string</span> outputText { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Dictionary</span>&lt;<span class="hljs-title">string</span>, <span class="hljs-title">LanguageDetails</span>&gt; LanguageList</span> = <span class="hljs-keyword">new</span> Dictionary&lt;<span class="hljs-keyword">string</span>, LanguageDetails&gt;();

    <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">async</span> Task <span class="hljs-title">OnInitializedAsync</span>(<span class="hljs-params"></span>)</span>
    {
        availableLanguages = <span class="hljs-keyword">await</span> translationService.GetAvailableLanguages();
        LanguageList = availableLanguages.Translation;
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">SelectLanguage</span>(<span class="hljs-params">ChangeEventArgs langEvent</span>)</span>
    {
        <span class="hljs-keyword">this</span>.outputLanguage = langEvent.Value.ToString();
    }

    <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">async</span> Task <span class="hljs-title">Translate</span>(<span class="hljs-params"></span>)</span>
    {
        <span class="hljs-keyword">if</span> (!<span class="hljs-keyword">string</span>.IsNullOrEmpty(outputLanguage))
        {
            translations = <span class="hljs-keyword">await</span> translationService.GetTranslatation(<span class="hljs-keyword">this</span>.inputText, <span class="hljs-keyword">this</span>.outputLanguage);
            outputText = translations[<span class="hljs-number">0</span>].Translations[<span class="hljs-number">0</span>].Text;
            inputLanguage = translations[<span class="hljs-number">0</span>].DetectedLanguage.Language;
        }
    }
}
</code></pre>
<p>We are invoking the <code>GetAvailableLanguages</code> method from our service inside the <code>OnInitializedAsync</code>. This <code>OnInitializedAsync</code> is a lifecycle method that will be invoked upon component initialization. This will make sure that the language dropdown will be populated as the page loads.</p>
<p>The <code>SelectLanguage</code> method will set the <code>outputLanguage</code> for the translation. The Translate method will invoke the <code>GetTranslatation</code> method from the service. We will set the <code>outputText</code> and the language detected for the <code>inputLanguage</code> as returned from the service.</p>
<h2 id="heading-add-styling-for-the-translator-component">Add styling for the Translator component</h2>
<p>Navigate to <code>[BlazorTranslator\wwwroot\css\site.css](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/wwwroot/css/site.css#L185-L187)</code> file and put the following style definition inside it.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.translation-box</span> {
    <span class="hljs-attribute">margin</span>: <span class="hljs-number">15px</span> <span class="hljs-number">0px</span>;
}
</code></pre>
<h2 id="heading-adding-link-to-navigation-menu">Adding Link to Navigation menu</h2>
<p>The last step is to add the link of our Translator component in the navigation menu. Open <code>[BlazorTranslator/Shared/NavMenu.razor](https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services/blob/master/BlazorTranslator/Shared/NavMenu.razor#L15-L19)</code> file and add the following code into it.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item px-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"translator"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"oi oi-list-rich"</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Translator
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<p>Remove the navigation links for Counter and Fetch-data components as they are not required for this application.</p>
<h2 id="heading-execution-demo">Execution Demo</h2>
<p>Press F5 to launch the application. Click on the Translator button on the nav menu in the left. You can perform the multilanguage translation as shown in the image below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorTranslator-1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-summary">Summary</h2>
<p>We have created a Translator Text Cognitive Services resource on Azure. We have used the Translator Text API to create a multilanguage translator using Blazor. This translator supports more than 60 languages for translation. We fetched the list of supported languages for translation from the global API endpoint for Translator Text.</p>
<p>Get the Source code from <a target="_blank" href="https://github.com/AnkitSharma-007/Blazor-Translator-Azure-Cognitive-Services">GitHub</a> and play around to get a better understanding.</p>
<h2 id="heading-see-also">See Also</h2>
<ul>
<li><a target="_blank" href="https://ankitsharmablogs.com/optical-character-reader-using-blazor-and-computer-vision/">Optical Character Reader Using Blazor And Computer Vision</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/facebook-authentication-and-authorization-in-server-side-blazor-app/">Facebook Authentication And Authorization In Server-Side Blazor App</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/google-authentication-and-authorization-in-server-side-blazor-app/">Google Authentication And Authorization In Server-Side Blazor App</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/blazor-crud-using-google-cloud-firestore/">Blazor CRUD Using Google Cloud Firestore</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/hosting-a-blazor-application-on-firebase/">Hosting A Blazor Application on Firebase</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/localization-in-angular-using-i18n-tools/">Localization In Angular Using i18n Tools</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create an Optical Character Reader Using Blazor and Azure Computer Vision ]]>
                </title>
                <description>
                    <![CDATA[ By Ankit Sharma Introduction In this article, we will create an optical character recognition (OCR) application using Blazor and the Azure Computer Vision Cognitive Service.  Computer Vision is an AI service that analyzes content in images. We will u... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-an-optical-character-reader-using-blazor-and-azure-computer-vision/</link>
                <guid isPermaLink="false">66d45dae33b83c4378a517ba</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Azure ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Blazor ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Computer Vision ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 03 Mar 2020 18:48:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9c4c740569d1a4ca3143.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Ankit Sharma</p>
<h2 id="heading-introduction">Introduction</h2>
<p>In this article, we will create an optical character recognition (OCR) application using Blazor and the Azure Computer Vision Cognitive Service. </p>
<p>Computer Vision is an AI service that analyzes content in images. We will use the OCR feature of Computer Vision to detect the printed text in an image. </p>
<p>The application will extract the text from the image and detects the language of the text. Currently, the OCR API supports 25 languages.</p>
<p>A demo of the application is shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorComputerVision-1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<ul>
<li>Install the latest .NET Core 3.1 SDK from <a target="_blank" href="https://dotnet.microsoft.com/download/dotnet-core/3.1">https://dotnet.microsoft.com/download/dotnet-core/3.1</a></li>
<li>Install the latest version of Visual Studio 2019 from <a target="_blank" href="https://visualstudio.microsoft.com/downloads/">https://visualstudio.microsoft.com/downloads/</a></li>
<li>An Azure subscription account. You can create a free Azure account  at <a target="_blank" href="https://azure.microsoft.com/en-in/free/">https://azure.microsoft.com/en-in/free/</a></li>
</ul>
<h2 id="heading-image-requirements">Image requirements</h2>
<p>The OCR API will work on images that meet the requirements as mentioned below:</p>
<ul>
<li>The format of the image must be JPEG, PNG, GIF, or BMP.</li>
<li>The size of the image must be between 50 x 50 and 4200 x 4200 pixels.</li>
<li>The image file size should be less than 4 MB.</li>
<li>The text in the image can be rotated by any multiple of 90 degrees plus a small angle of up to 40 degrees.</li>
</ul>
<h2 id="heading-source-code">Source Code</h2>
<p>You can get the source code from <a target="_blank" href="https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services">GitHub</a>.</p>
<h2 id="heading-create-the-azure-computer-vision-cognitive-service-resource">Create the Azure Computer Vision Cognitive Service resource</h2>
<p>Log in to the Azure portal and search for the cognitive services in the search bar and click on the result. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/CreateTextCogServ.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On the next screen, click on the Add button. It will open the cognitive services marketplace page. </p>
<p>Search for the Computer Vision in the search bar and click on the search result. It will open the Computer Vision API page. Click on the Create button to create a new Computer Vision resource. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/SelectComputerVisionCogServ-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On the Create page, fill in the details as indicated below.</p>
<ul>
<li><strong>Name</strong>: Give a unique name for your resource.</li>
<li><strong>Subscription</strong>: Select the subscription type from the dropdown.</li>
<li><strong>Pricing tier</strong>: Select the pricing tier as per your choice.</li>
<li><strong>Resource group</strong>: Select an existing resource group or create a new one.</li>
</ul>
<p>Click on the Create button. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorConfigureComputerVisionCogServ.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After your resource is successfully deployed, click on the “Go to resource” button. You can see the Key and the endpoint for the newly created Computer Vision resource. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/ComputerVisionCogServKey.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Make a note of the key and the endpoint. We will be using these in the latter part of this article to invoke the Computer Vision OCR API from the .NET Code. The values are masked here for privacy.</p>
<h2 id="heading-create-a-server-side-blazor-application">Create a Server-Side Blazor Application</h2>
<p>Open Visual Studio 2019, click on “Create a new project”. Select “Blazor App” and click on the “Next” button. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/CreateProject.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>On the next window, put the project name as <code>BlazorComputerVision</code> and click on the “Create” button. </p>
<p>The next window will ask you to select the type of Blazor app. Select <code>Blazor Server App</code> and click on the Create button to create a new server-side Blazor application. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorTemplate.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-installing-computer-vision-api-library">Installing Computer Vision API library</h2>
<p>We will install the Azure Computer Vision API library which will provide us with the models out of the box to handle the Computer Vision REST API response. </p>
<p>To install the package, navigate to Tools &gt;&gt; NuGet Package Manager &gt;&gt; Package Manager Console. It will open the Package Manager Console. Run the command as shown below.</p>
<pre><code>Install-Package Microsoft.Azure.CognitiveServices.Vision.ComputerVision -Version <span class="hljs-number">5.0</span><span class="hljs-number">.0</span>
</code></pre><p>You can learn more about this package at the <a target="_blank" href="https://www.nuget.org/packages/Microsoft.Azure.CognitiveServices.Vision.ComputerVision/">NuGet gallery</a>.</p>
<h2 id="heading-create-the-models">Create the Models</h2>
<p>Right-click on the <code>BlazorComputerVision</code> project and select Add &gt;&gt; New Folder. Name the folder as <code>Models</code>. Again, right-click on the <code>Models</code> folder and select Add &gt;&gt; Class to add a new class file. Put the name of your class as <code>LanguageDetails.cs</code> and click Add.</p>
<p>Open <code>[LanguageDetails.cs](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Models/LanguageDetails.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorComputerVision.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">LanguageDetails</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> NativeName { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Dir { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Similarly, add a new class file <code>[AvailableLanguage.cs](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Models/AvailableLanguage.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">using</span> System.Collections.Generic;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorComputerVision.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">AvailableLanguage</span>
    {
        <span class="hljs-keyword">public</span> Dictionary&lt;<span class="hljs-keyword">string</span>, LanguageDetails&gt; Translation { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>Finally, we will add a class as DTO (Data Transfer Object) for sending data back to the client. Add a new class file <code>[OcrResultDTO.cs](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Models/OcrResultDTO.cs)</code> and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorComputerVision.Models</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">OcrResultDTO</span>
    {
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Language { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }

        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> DetectedText { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<h2 id="heading-create-the-computer-vision-service">Create the Computer Vision Service</h2>
<p>Right-click on the <code>BlazorComputerVision/Data</code> folder and select Add &gt;&gt; Class to add a new class file. Put the name of the file as <code>ComputerVisionService.cs</code> and click on Add.</p>
<p>Open the <code>[ComputerVisionService.cs](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Data/ComputerVisionService.cs)</code> file and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">using</span> BlazorComputerVision.Models;
<span class="hljs-keyword">using</span> Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
<span class="hljs-keyword">using</span> Newtonsoft.Json;
<span class="hljs-keyword">using</span> Newtonsoft.Json.Linq;
<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Net.Http;
<span class="hljs-keyword">using</span> System.Net.Http.Headers;
<span class="hljs-keyword">using</span> System.Text;
<span class="hljs-keyword">using</span> System.Threading.Tasks;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorComputerVision.Data</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">ComputerVisionService</span>
    {
        <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> subscriptionKey;
        <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> endpoint;
        <span class="hljs-keyword">static</span> <span class="hljs-keyword">string</span> uriBase;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ComputerVisionService</span>(<span class="hljs-params"></span>)</span>
        {
            subscriptionKey = <span class="hljs-string">"b993f3afb4e04119bd8ed37171d4ec71"</span>;
            endpoint = <span class="hljs-string">"https://ankitocrdemo.cognitiveservices.azure.com/"</span>;
            uriBase = endpoint + <span class="hljs-string">"vision/v2.1/ocr"</span>;
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task&lt;OcrResultDTO&gt; <span class="hljs-title">GetTextFromImage</span>(<span class="hljs-params"><span class="hljs-keyword">byte</span>[] imageFileBytes</span>)</span>
        {
            StringBuilder sb = <span class="hljs-keyword">new</span> StringBuilder();
            OcrResultDTO ocrResultDTO = <span class="hljs-keyword">new</span> OcrResultDTO();
            <span class="hljs-keyword">try</span>
            {
                <span class="hljs-keyword">string</span> JSONResult = <span class="hljs-keyword">await</span> ReadTextFromStream(imageFileBytes);

                OcrResult ocrResult = JsonConvert.DeserializeObject&lt;OcrResult&gt;(JSONResult);

                <span class="hljs-keyword">if</span> (!ocrResult.Language.Equals(<span class="hljs-string">"unk"</span>))
                {
                    <span class="hljs-keyword">foreach</span> (OcrLine ocrLine <span class="hljs-keyword">in</span> ocrResult.Regions[<span class="hljs-number">0</span>].Lines)
                    {
                        <span class="hljs-keyword">foreach</span> (OcrWord ocrWord <span class="hljs-keyword">in</span> ocrLine.Words)
                        {
                            sb.Append(ocrWord.Text);
                            sb.Append(<span class="hljs-string">' '</span>);
                        }
                        sb.AppendLine();
                    }
                }
                <span class="hljs-keyword">else</span>
                {
                    sb.Append(<span class="hljs-string">"This language is not supported."</span>);
                }
                ocrResultDTO.DetectedText = sb.ToString();
                ocrResultDTO.Language = ocrResult.Language;
                <span class="hljs-keyword">return</span> ocrResultDTO;
            }
            <span class="hljs-keyword">catch</span>
            {
                ocrResultDTO.DetectedText = <span class="hljs-string">"Error occurred. Try again"</span>;
                ocrResultDTO.Language = <span class="hljs-string">"unk"</span>;
                <span class="hljs-keyword">return</span> ocrResultDTO;
            }
        }

        <span class="hljs-function"><span class="hljs-keyword">static</span> <span class="hljs-keyword">async</span> Task&lt;<span class="hljs-keyword">string</span>&gt; <span class="hljs-title">ReadTextFromStream</span>(<span class="hljs-params"><span class="hljs-keyword">byte</span>[] byteData</span>)</span>
        {
            <span class="hljs-keyword">try</span>
            {
                HttpClient client = <span class="hljs-keyword">new</span> HttpClient();
                client.DefaultRequestHeaders.Add(<span class="hljs-string">"Ocp-Apim-Subscription-Key"</span>, subscriptionKey);
                <span class="hljs-keyword">string</span> requestParameters = <span class="hljs-string">"language=unk&amp;detectOrientation=true"</span>;
                <span class="hljs-keyword">string</span> uri = uriBase + <span class="hljs-string">"?"</span> + requestParameters;
                HttpResponseMessage response;

                <span class="hljs-keyword">using</span> (ByteArrayContent content = <span class="hljs-keyword">new</span> ByteArrayContent(byteData))
                {
                    content.Headers.ContentType = <span class="hljs-keyword">new</span> MediaTypeHeaderValue(<span class="hljs-string">"application/octet-stream"</span>);
                    response = <span class="hljs-keyword">await</span> client.PostAsync(uri, content);
                }

                <span class="hljs-keyword">string</span> contentString = <span class="hljs-keyword">await</span> response.Content.ReadAsStringAsync();
                <span class="hljs-keyword">string</span> result = JToken.Parse(contentString).ToString();
                <span class="hljs-keyword">return</span> result;
            }
            <span class="hljs-keyword">catch</span> (Exception e)
            {
                <span class="hljs-keyword">return</span> e.Message;
            }
        }

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task&lt;AvailableLanguage&gt; <span class="hljs-title">GetAvailableLanguages</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">string</span> endpoint = <span class="hljs-string">"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&amp;scope=translation"</span>;
            <span class="hljs-keyword">var</span> client = <span class="hljs-keyword">new</span> HttpClient();
            <span class="hljs-keyword">using</span> (<span class="hljs-keyword">var</span> request = <span class="hljs-keyword">new</span> HttpRequestMessage())
            {
                request.Method = HttpMethod.Get;
                request.RequestUri = <span class="hljs-keyword">new</span> Uri(endpoint);
                <span class="hljs-keyword">var</span> response = <span class="hljs-keyword">await</span> client.SendAsync(request).ConfigureAwait(<span class="hljs-literal">false</span>);
                <span class="hljs-keyword">string</span> result = <span class="hljs-keyword">await</span> response.Content.ReadAsStringAsync();

                AvailableLanguage deserializedOutput = JsonConvert.DeserializeObject&lt;AvailableLanguage&gt;(result);

                <span class="hljs-keyword">return</span> deserializedOutput;
            }
        }
    }
}
</code></pre>
<p>In the constructor of the class, we have initialized the key and the endpoint URL for the OCR API.</p>
<p>Inside the <code>ReadTextFromStream</code> method, we will create a new <code>HttpRequestMessage</code>. This HTTP request is a Post request. We will pass the subscription key in the header of the request. The OCR API will return a JSON object having each word from the image as well as the detected language of the text.</p>
<p>The <code>GetTextFromImage</code> method will accept the image data as a byte array and returns an object of type <code>OcrResultDTO</code>. We will invoke the <code>ReadTextFromStream</code> method and deserialize the response into an object of type <code>OcrResult</code>. We will then form the sentence by iterating over the <code>OcrWord</code> object.</p>
<p>The <code>GetAvailableLanguages</code> method will return the list of all the language supported by the Translate Text API. We will set the request URI and create a <code>HttpRequestMessage</code> which will be a Get request. This request URL will return a JSON object which will be deserialized to an object of type <code>AvailableLanguage</code>.</p>
<h2 id="heading-why-do-we-need-to-fetch-the-list-of-supported-languages">Why do we need to fetch the list of supported languages?</h2>
<p>The OCR API returns the language code (e.g. en for English, de for German, etc.) of the detected language. But we cannot display the language code on the UI as it is not user-friendly. Therefore, we need a dictionary to look up the language name corresponding to the language code.</p>
<p>The Azure Computer Vision OCR API supports 25 languages. To know all the languages supported by OCR API see the list of <a target="_blank" href="https://docs.microsoft.com/en-us/azure/cognitive-services/computer-vision/language-support">supported languages</a>. </p>
<p>These languages are a subset of the languages supported by the Azure Translate Text API. Since there is no dedicated API endpoint to fetch the list of languages supported by OCR API, therefore, we are using the Translate Text API endpoint to fetch the list of languages. </p>
<p>We will create the language lookup dictionary using the JSON response from this API call and filter the result based on the language code returned by the OCR API.</p>
<h2 id="heading-install-blazorinputfile-nuget-package">Install BlazorInputFile NuGet package</h2>
<p><a target="_blank" href="https://www.nuget.org/packages/BlazorInputFile/">BlazorInputFile</a> is a file input component for Blazor applications. It provides the ability to upload single or multiple files to a Blazor app.</p>
<p>Open <code>[BlazorComputerVision.csproj](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/BlazorComputerVision.csproj#L8)</code> file and add a dependency for the <code>BlazorInputFile</code> package as shown below:</p>
<pre><code class="lang-xhtml"><span class="hljs-tag">&lt;<span class="hljs-name">ItemGroup</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">PackageReference</span> <span class="hljs-attr">Include</span>=<span class="hljs-string">"BlazorInputFile"</span> <span class="hljs-attr">Version</span>=<span class="hljs-string">"0.1.0-preview-00002"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">ItemGroup</span>&gt;</span>
</code></pre>
<p>Open <code>[BlazorComputerVision\Pages\_Host.cshtml](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Pages/_Host.cshtml#L17)</code> file and add the reference for the package’s JavaScript file by adding the following line in the <code>&lt;head&gt;</code> section.</p>
<pre><code class="lang-js">&lt;script src=<span class="hljs-string">"_content/BlazorInputFile/inputfile.js"</span>&gt;&lt;/script&gt;
</code></pre>
<p>Add the following line in the <code>[_Imports.razor](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/_Imports.razor#L10)</code> file.</p>
<pre><code>@using BlazorInputFile
</code></pre><h2 id="heading-configuring-the-service"><strong>Configuring the Service</strong></h2>
<p>To make the service available to the components we need to configure it on the server-side app. Open the Startup.cs file. Add the following line inside the <code>[ConfigureServices](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Startup.cs#L31)</code> method of Startup class.</p>
<pre><code class="lang-c#"> services.AddSingleton&lt;ComputerVisionService&gt;();
</code></pre>
<h2 id="heading-creating-the-blazor-ui-component">Creating the Blazor UI Component</h2>
<p>We will add the Razor page in the <code>BlazorComputerVision/Pages</code> folder. By default, we have “Counter” and “Fetch Data” pages provided in our application. These default pages will not affect our application but for the sake of this tutorial, we will delete fetchdata and counter pages from <code>BlazorComputerVision/Pages</code> folder.</p>
<p>Right-click on the <code>BlazorComputerVision/Pages</code> folder and then select Add &gt;&gt; New Item. An “Add New Item” dialog box will open, select “Visual C#” from the left panel, then select “Razor Component” from the templates panel, put the name as <code>OCR.razor</code>. Click Add. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorOCRComponent.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>We will add a code-behind file for this razor page to keep the code and presentation separate. This will allow easy maintenance for the application.  </p>
<p>Right-click on the <code>BlazorComputerVision/Pages</code> folder and then select Add &gt;&gt; Class. Name the class as <code>OCR.razor.cs</code>. The Blazor framework is smart enough to tag this class file to the razor file. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/BlazorCodeBehind.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-blazor-ui-component-code-behind">Blazor UI component code behind</h2>
<p>Open the <code>[OCR.razor.cs](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Pages/OCR.razor.cs)</code> file and put the following code inside it.</p>
<pre><code class="lang-c#"><span class="hljs-keyword">using</span> Microsoft.AspNetCore.Components;
<span class="hljs-keyword">using</span> System;
<span class="hljs-keyword">using</span> System.Collections.Generic;
<span class="hljs-keyword">using</span> System.Linq;
<span class="hljs-keyword">using</span> System.Threading.Tasks;
<span class="hljs-keyword">using</span> System.IO;
<span class="hljs-keyword">using</span> BlazorComputerVision.Models;
<span class="hljs-keyword">using</span> BlazorInputFile;
<span class="hljs-keyword">using</span> BlazorComputerVision.Data;

<span class="hljs-keyword">namespace</span> <span class="hljs-title">BlazorComputerVision.Pages</span>
{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">OCRModel</span> : <span class="hljs-title">ComponentBase</span>
    {
        [<span class="hljs-meta">Inject</span>]
        <span class="hljs-keyword">protected</span> ComputerVisionService computerVisionService { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }

        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">string</span> DetectedTextLanguage;
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">string</span> imagePreview;
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">bool</span> loading = <span class="hljs-literal">false</span>;
        <span class="hljs-keyword">byte</span>[] imageFileBytes;

        <span class="hljs-keyword">const</span> <span class="hljs-keyword">string</span> DefaultStatus = <span class="hljs-string">"Maximum size allowed for the image is 4 MB"</span>;
        <span class="hljs-keyword">protected</span> <span class="hljs-keyword">string</span> status = DefaultStatus;

        <span class="hljs-keyword">protected</span> OcrResultDTO Result = <span class="hljs-keyword">new</span> OcrResultDTO();
        <span class="hljs-keyword">private</span> AvailableLanguage availableLanguages;
        <span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-title">Dictionary</span>&lt;<span class="hljs-title">string</span>, <span class="hljs-title">LanguageDetails</span>&gt; LanguageList</span> = <span class="hljs-keyword">new</span> Dictionary&lt;<span class="hljs-keyword">string</span>, LanguageDetails&gt;();
        <span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> MaxFileSize = <span class="hljs-number">4</span> * <span class="hljs-number">1024</span> * <span class="hljs-number">1024</span>; <span class="hljs-comment">// 4MB</span>

        <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">override</span> <span class="hljs-keyword">async</span> Task <span class="hljs-title">OnInitializedAsync</span>(<span class="hljs-params"></span>)</span>
        {
            availableLanguages = <span class="hljs-keyword">await</span> computerVisionService.GetAvailableLanguages();
            LanguageList = availableLanguages.Translation;
        }

        <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">async</span> Task <span class="hljs-title">ViewImage</span>(<span class="hljs-params">IFileListEntry[] files</span>)</span>
        {
            <span class="hljs-keyword">var</span> file = files.FirstOrDefault();
            <span class="hljs-keyword">if</span> (file == <span class="hljs-literal">null</span>)
            {
                <span class="hljs-keyword">return</span>;
            }
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (file.Size &gt; MaxFileSize)
            {
                status = <span class="hljs-string">$"The file size is <span class="hljs-subst">{file.Size}</span> bytes, this is more than the allowed limit of <span class="hljs-subst">{MaxFileSize}</span> bytes."</span>;
                <span class="hljs-keyword">return</span>;
            }
            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (!file.Type.Contains(<span class="hljs-string">"image"</span>))
            {
                status = <span class="hljs-string">"Please uplaod a valid image file"</span>;
                <span class="hljs-keyword">return</span>;
            }
            <span class="hljs-keyword">else</span>
            {
                <span class="hljs-keyword">var</span> memoryStream = <span class="hljs-keyword">new</span> MemoryStream();
                <span class="hljs-keyword">await</span> file.Data.CopyToAsync(memoryStream);
                imageFileBytes = memoryStream.ToArray();
                <span class="hljs-keyword">string</span> base64String = Convert.ToBase64String(imageFileBytes, <span class="hljs-number">0</span>, imageFileBytes.Length);

                imagePreview = <span class="hljs-keyword">string</span>.Concat(<span class="hljs-string">"data:image/png;base64,"</span>, base64String);
                memoryStream.Flush();
                status = DefaultStatus;
            }
        }

        <span class="hljs-function"><span class="hljs-keyword">protected</span> <span class="hljs-keyword">private</span> <span class="hljs-keyword">async</span> Task <span class="hljs-title">GetText</span>(<span class="hljs-params"></span>)</span>
        {
            <span class="hljs-keyword">if</span> (imageFileBytes != <span class="hljs-literal">null</span>)
            {
                loading = <span class="hljs-literal">true</span>;
                Result = <span class="hljs-keyword">await</span> computerVisionService.GetTextFromImage(imageFileBytes);
                <span class="hljs-keyword">if</span> (LanguageList.ContainsKey(Result.Language))
                {
                    DetectedTextLanguage = LanguageList[Result.Language].Name;
                }
                <span class="hljs-keyword">else</span>
                {
                    DetectedTextLanguage = <span class="hljs-string">"Unknown"</span>;
                }
                loading = <span class="hljs-literal">false</span>;
            }
        }
    }
}
</code></pre>
<p>We are injecting the <code>ComputerVisionService</code> in this class.</p>
<p>The <code>OnInitializedAsync</code> is a Blazor lifecycle method which is invoked when the component is initialized. We are invoking the <code>GetAvailableLanguages</code> method from our service inside the <code>OnInitializedAsync</code> method. We will then initialize the variable LanguageList which is a dictionary to hold the details of available languages.</p>
<p>Inside the <code>ViewImage</code> method, we will check if the uploaded file is an image only and the size is less than 4 MB. We will transfer the uploaded image to the memory stream. We will then convert that memory stream to a byte array. </p>
<p>To set the image preview, we will convert the image from byte array to a base64 encoded string. The <code>GetText</code> method will invoke the <code>GetTextFromImage</code> method from the service and pass the image byte array as an argument. We will search for the language name from the dictionary based on the language code returned from the service. If no language code is available, we will set the language as unknown.</p>
<h2 id="heading-blazor-ui-component-template">Blazor UI component template</h2>
<p>Open the <code>[OCR.razor](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Pages/OCR.razor)</code> file and put the following code inside it.</p>
<pre><code class="lang-html">@page "/computer-vision-ocr"
@inherits OCRModel

<span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>Optical Character Recognition (OCR) Using Blazor and Azure Computer Vision Cognitive Service<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"col-md-5"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">disabled</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"10"</span> <span class="hljs-attr">cols</span>=<span class="hljs-string">"15"</span>&gt;</span>@Result.DetectedText<span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"row"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"col-sm-5"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">label</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">strong</span>&gt;</span> Detected Language :<span class="hljs-tag">&lt;/<span class="hljs-name">strong</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"col-sm-6"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">disabled</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form-control"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"@DetectedTextLanguage"</span> /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"col-md-5"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"image-container"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"preview-image"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">@imagePreview</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">InputFile</span> <span class="hljs-attr">OnChange</span>=<span class="hljs-string">"ViewImage"</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>@status<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">disabled</span>=<span class="hljs-string">"@loading"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn btn-primary btn-lg"</span> @<span class="hljs-attr">onclick</span>=<span class="hljs-string">"GetText"</span>&gt;</span>
            @if (loading)
            {
                <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"spinner-border spinner-border-sm mr-1"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
            }
            Extract Text
        <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We have defined the route for this component. We have inherited the <code>OCRModel</code> class which allows us to access the properties and method of this class from the template. Bootstrap is used for designing the UI. We have a text area to display the detected text and a text box to display the detected language. The image tag is used to show the image preview after uploading the image. The <code>&lt;InputFile&gt;</code> component will allow us to upload an image file and invoke the <code>ViewImage</code> method as we upload the image.</p>
<h2 id="heading-add-styling-for-the-component">Add styling for the component</h2>
<p>Navigate to <code>[BlazorComputerVision\wwwroot\css\site.css](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/wwwroot/css/site.css#L185-L197)</code> file and add the following style definition inside it.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.preview-image</span> {
    <span class="hljs-attribute">max-height</span>: <span class="hljs-number">300px</span>;
    <span class="hljs-attribute">max-width</span>: <span class="hljs-number">300px</span>;
}
<span class="hljs-selector-class">.image-container</span> {
    <span class="hljs-attribute">display</span>: flex;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span>;
    <span class="hljs-attribute">align-content</span>: center;
    <span class="hljs-attribute">align-items</span>: center;
    <span class="hljs-attribute">justify-content</span>: center;
    <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> dashed skyblue;
}
</code></pre>
<h2 id="heading-adding-link-to-navigation-menu"><strong>Adding Link to Navigation menu</strong></h2>
<p>The last step is to add the link of our OCR component in the navigation menu. Open <code>[BlazorComputerVision/Shared/NavMenu.razor](https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services/blob/master/BlazorComputerVision/Shared/NavMenu.razor#L15-L19)</code> file and add the following code into it.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-item px-3"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">NavLink</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav-link"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"computer-vision-ocr"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"oi oi-list-rich"</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> Computer Vision
    <span class="hljs-tag">&lt;/<span class="hljs-name">NavLink</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
</code></pre>
<p>Remove the navigation links for Counter and Fetch-data components as they are not required for this application.</p>
<h2 id="heading-execution-demo">Execution Demo</h2>
<p>Press F5 to launch the application. Click on the Computer Vision button on the nav menu on the left. On the next page, upload an image with some text in it and click on the “Extract Text” button. You will see the extracted text in the text area on the left along with the detected language for the text. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/Execution_English.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now we will try to upload an image with some French text on it, you can see the extracted text and the detected language as French. Refer to the image shown below.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/Execution_French.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If we try to upload an image with an unsupported language, we will get the error. Refer to the image shown below where an image with text written in Hindi is uploaded.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/03/Execution_Hindi.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-summary"><strong>Summary</strong></h2>
<p>We have created an optical character recognition (OCR) application using Blazor and the Computer Vision Azure Cognitive Service. We have added the feature of uploading an image file using the <code>BlazorInputFile</code> component. The application is able to extract the printed text from the uploaded image and recognizes the language of the text. The OCR API of the Computer Vision is used which can recognize text in 25 languages.</p>
<p>Get the Source code from <a target="_blank" href="https://github.com/AnkitSharma-007/Blazor-Computer-Vision-Azure-Cognitive-Services">GitHub</a> and play around to get a better understanding.</p>
<h2 id="heading-see-also">See Also</h2>
<ul>
<li><a target="_blank" href="https://ankitsharmablogs.com/multi-language-translator-using-blazor-and-azure-cognitive-services/">Multi-Language Translator Using Blazor And Azure Cognitive Services</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/facebook-authentication-and-authorization-in-server-side-blazor-app/">Facebook Authentication And Authorization In Server-Side Blazor App</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/google-authentication-and-authorization-in-server-side-blazor-app/">Google Authentication And Authorization In Server-Side Blazor App</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/policy-based-authorization-in-angular-using-jwt/">Policy-Based Authorization In Angular Using JWT</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/continuous-deployment-for-angular-app-using-heroku-and-github/">Continuous Deployment For Angular App Using Heroku And GitHub</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/hosting-a-blazor-application-on-firebase/">Hosting A Blazor Application on Firebase</a></li>
<li><a target="_blank" href="https://ankitsharmablogs.com/deploying-a-blazor-application-on-azure/">Deploying A Blazor Application On Azure</a></li>
</ul>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ I rebuilt the same web API using Express, Flask, and ASP.NET. Here's what I found. ]]>
                </title>
                <description>
                    <![CDATA[ By M. S. Farzan I've been shopping around for a back end framework to support a tabletop game app, and decided to do some research to determine the best fit for my needs.  The objective was straightforward: to build a simple RESTful API that would al... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/i-built-a-web-api-with-express-flask-aspnet/</link>
                <guid isPermaLink="false">66d851f5248f4119381de123</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Back end development  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ backend ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Backend Development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C# ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Express ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Flask Framework ]]>
                    </category>
                
                    <category>
                        <![CDATA[ full stack ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ REST API ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Sat, 29 Feb 2020 19:00:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9c5c740569d1a4ca31ac.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By M. S. Farzan</p>
<p>I've been shopping around for a back end framework to support a <a target="_blank" href="https://www.nightpathpub.com/entromancy">tabletop game app</a>, and decided to do some research to determine the best fit for my needs. </p>
<p>The objective was straightforward: to build a simple <a target="_blank" href="https://restfulapi.net/">RESTful API</a> that would allow a front end app to perform basic <a target="_blank" href="https://en.wikipedia.org/wiki/Create,_read,_update_and_delete">CRUD</a> operations, providing me with an introduction to what the development process would look like.</p>
<p>There are a lot of back end framework options out there, and I'm most familiar with JavaScript, C#, and Python (in that order), which limited my options somewhat.  The natural starting point was to build a simple front end that would send requests to an API, which would in turn read from and write to a local database.</p>
<p>I began my development process with Express, which, for reasons I'll soon explain, led me to also check out Flask and ASP.NET. I thought my findings might prove useful to others out there who are researching back end frameworks for small projects. In this article, I'll also provide code examples and the resources that I used to build everything.</p>
<p>You can access the full code on <a target="_blank" href="https://github.com/sominator/web-api-project">GitHub</a>, as well.</p>
<p>I should caveat that I won't be promoting one framework over another, and haven't yet compared things like deployment, authentication, or scalability.  Your mileage may vary if those particulars are important to you!</p>
<p>I will, however, provide a <strong>TL;DR</strong> at the bottom if you just want to get the summary and key learnings.</p>
<p>Here we go!</p>
<h2 id="heading-defining-the-api">Defining the API</h2>
<p>If you're new to web development, you might be asking, "what's an API?"</p>
<p>I've had to ask the question a hundred times to find an answer that made sense. And it really wasn't until I built my own that I could say I understood what an API <em>does</em>.</p>
<p>Put simply, an API, or "application programming interface", allows two different computing systems to talk to one another. In this article, I'll show a simple front end app that displays a "quest" tracker that players can view for their tabletop roleplaying game. Each quest has a "name" and a "description," both of which are displayed in the web browser.</p>
<p>If I already had all of the quests listed on the website and just wanted players to view them, I would have no need for an API or back end. For this project, however, I want the ability to allow users to add quests, search for them, delete them, and so on. For those operations, I need to store the quests somewhere, but my front end app isn't able to transfer information directly to a database.</p>
<p>For that, I need an API that can receive HTTP requests from the website, figure out what to do with those requests, interact with my database, and send more information back up the chain so that the user can see what happened.</p>
<p>The whole thing - the front end "client", the back end "API" or server, and the database - is called a "stack," or more precisely, the "full stack." For this project, I built a simple front end website as the top of the stack, and switched out everything beneath it as I tried out different frameworks and databases.</p>
<h2 id="heading-project-structure">Project Structure</h2>
<p>The structure for this project was fairly simple, with the front end client separated from three different servers that I would spin up as necessary to serve the API.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/Project-Structure.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>I used <a target="_blank" href="https://visualstudio.microsoft.com/vs/community/">Visual Studio Community</a> as my code editor and IDE, with the requisite language packages installed for JavaScript, Python, and C#.</p>
<p>I'll provide an overview of my experience with each framework in turn, with links to the tutorials and packages that I used to get them to work with the client. But first, let's take a look at the front end!</p>
<h2 id="heading-the-client-vuejs">The Client: Vue.js</h2>
<p>The goal for the client was to have a simple website that would receive information from the database through the API and display it to the user. To streamline the process, my requirements were that the client would only need to "read" all of the items in the database, and provide the user with the ability to "create" a new quest.  </p>
<p>These "read" and "create" operations - the "R" and "C" in "CRUD" - are analogous to the HTTP methods of "GET" and "POST," which we'll see in the code below.</p>
<p>In front end development, I'm most comfortable using <a target="_blank" href="https://vuejs.org/">Vue</a>, and used the <a target="_blank" href="https://cli.vuejs.org/">Vue CLI</a> to scaffold a basic client, with the following file structure:   </p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/Client-Structure.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>I replaced the boilerplate markup provided by the Vue CLI with the following:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">template</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"app"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>RPG Quests<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">v-for</span>=<span class="hljs-string">"(quest, index) in quests"</span> <span class="hljs-attr">v-bind:key</span>=<span class="hljs-string">"index"</span>&gt;</span>{{quest.name}}: {{quest.description}}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"newQuestName"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Quest Name"</span> /&gt;</span> <span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">v-model</span>=<span class="hljs-string">"newQuestDescription"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Quest Description"</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">br</span> /&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">v-on:click</span>=<span class="hljs-string">"postQuest"</span>&gt;</span>Add Quest<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">template</span>&gt;</span>
</code></pre>
<p>And the corresponding Vue code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">'axios'</span>;

    <span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> {
        <span class="hljs-attr">name</span>: <span class="hljs-string">'App'</span>,
        <span class="hljs-attr">data</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-keyword">return</span> {
                <span class="hljs-attr">quests</span>: <span class="hljs-literal">null</span>,
                <span class="hljs-attr">newQuestName</span>: <span class="hljs-literal">null</span>,
                <span class="hljs-attr">newQuestDescription</span>: <span class="hljs-literal">null</span>
            }
        },
        <span class="hljs-attr">methods</span>: {
            <span class="hljs-attr">getQuests</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                axios
                    .get(<span class="hljs-string">'http://localhost:3000/quests'</span>)
                    .then(<span class="hljs-function"><span class="hljs-params">response</span> =&gt;</span> (<span class="hljs-built_in">this</span>.quests = response.data));
            },
            <span class="hljs-attr">addQuest</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                axios
                    .post(<span class="hljs-string">'http://localhost:3000/quests'</span>, {
                        <span class="hljs-attr">name</span>: <span class="hljs-built_in">this</span>.newQuestName,
                        <span class="hljs-attr">description</span>: <span class="hljs-built_in">this</span>.newQuestDescription
                    });
            },
            <span class="hljs-attr">postQuest</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
                axios.all([<span class="hljs-built_in">this</span>.addQuest(), <span class="hljs-built_in">this</span>.getQuests()]);
                <span class="hljs-built_in">this</span>.$forceUpdate();
            }
        },
        <span class="hljs-attr">mounted</span>: <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            <span class="hljs-built_in">this</span>.getQuests();
        }
    }
</code></pre>
<p>If you're not familiar with Vue, the specifics of the front end aren't that important! Of significance here is that I'm using a JavaScript package called <a target="_blank" href="https://github.com/axios/axios">Axios</a> to make my GET and POST requests to a potential server. </p>
<p>When the client loads, it'll make a GET request to the URL http://localhost:3000/quests to load all quests from the database. It also provides a couple of input fields and a button that will POST a new quest.</p>
<p>Using the Vue CLI to serve the client on http://localhost:8080, the front end of the app looks like this in action:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/Client.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Once quests are added to the database, they'll start appearing in between the "RPG Quests" header and the input fields.</p>
<h3 id="heading-client-resources">Client Resources</h3>
<p>To build the client, I used:</p>
<ul>
<li><a target="_blank" href="https://nodejs.org/en/">NodeJS</a>/<a target="_blank" href="https://www.npmjs.com/">NPM</a> for package management</li>
<li><a target="_blank" href="https://cli.vuejs.org/">Vue CLI</a> for scaffolding, serving, and building projects</li>
<li><a target="_blank" href="https://github.com/axios/axios">Axios</a> for making HTTP requests to the API</li>
<li><a target="_blank" href="https://vuejs.org/v2/cookbook/using-axios-to-consume-apis.html">Vue Axios Documentation</a> for making sense of how to use Axios in concert with the API</li>
<li><a target="_blank" href="https://www.postman.com/">Postman</a> for testing API requests through the browser before implementing them in the client.</li>
</ul>
<h2 id="heading-javascript-api-express">JavaScript API: Express</h2>
<p><a target="_blank" href="https://expressjs.com/">Express</a> is a lightweight web framework for <a target="_blank" href="https://nodejs.org/en/">NodeJS</a> that allows you to write server-side applications with JavaScript.</p>
<p>It's un-opinionated, which means that you can build your applications how you like without it defining the architecture for you. You can add packages to improve functionality as you fancy, which I found to be a double-edged sword as a newbie to the framework. More on that later.</p>
<p>Being most comfortable in JavaScript, I was excited by the prospect of having the entire stack run on just one language instead of several. I had heard of the "MEVN Stack," which denotes a full stack application that is comprised of <a target="_blank" href="https://www.mongodb.com/">MongoDB</a>, Express, Vue, and NodeJS, and decided to try that out for this iteration of the project.</p>
<p>I followed a <a target="_blank" href="https://dev.to/beznet/build-a-rest-api-with-node-express-mongodb-4ho4">web API tutorial</a> to first build a template app, then used another <a target="_blank" href="https://medium.com/@anaida07/mevn-stack-application-part-1-3a27b61dcae0">MEVN tutorial</a> to fill in the details of how to get the API to communicate with the Vue client that I had built. The Express API that I created for this project follows a similar structure to the former, using MongoDB as the database:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/Express-Structure.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>If you're coming from a JavaScript background, Express is fairly easy to read, even if you're not familiar with some of the back end terminology. The following is a snippet from /routes/quests.js, for example, which handles the HTTP <a target="_blank" href="https://en.wikipedia.org/wiki/Web_API#Endpoints">endpoint</a> requests:</p>
<pre><code class="lang-javascript">router.get(<span class="hljs-string">'/'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> quests = <span class="hljs-keyword">await</span> Quest.find();
        res.json(quests);
    } <span class="hljs-keyword">catch</span> (err) {
        res.status(<span class="hljs-number">500</span>).json({ <span class="hljs-attr">message</span>: err.message });
    }
});

router.post(<span class="hljs-string">'/'</span>, <span class="hljs-keyword">async</span> (req, res) =&gt; {
    <span class="hljs-keyword">const</span> quest = <span class="hljs-keyword">new</span> Quest({
        <span class="hljs-attr">name</span>: req.body.name,
        <span class="hljs-attr">description</span>: req.body.description
    });
    <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> newQuest = <span class="hljs-keyword">await</span> quest.save();
        res.status(<span class="hljs-number">201</span>).json(newQuest);
    } <span class="hljs-keyword">catch</span> (err) {
        res.status(<span class="hljs-number">400</span>).json({ <span class="hljs-attr">message</span>: err.message });
    }
});
</code></pre>
<p>The general theme of the code is to receive a request, attempt to contact the database to do work, and then send a response back to whoever's asking. The specifics can be quite complex, particularly if you're writing your own <a target="_blank" href="https://expressjs.com/en/guide/using-middleware.html">middleware</a> that does things in between the request and response, but the code is at least readable.</p>
<p>I found MongoDB to be painless to work with as a <a target="_blank" href="https://www.mongodb.com/nosql-explained">NoSQL</a> database.  If you're working with Express, you'll most likely use <a target="_blank" href="https://mongoosejs.com/">Mongoose</a> as an <a target="_blank" href="https://en.wikipedia.org/wiki/Object-relational_mapping#Object-oriented_databases">ODM</a> - basically like a "middle person" that translates a model of what your data looks like to the database.</p>
<p>The model in this app (called a "schema" in Mongoose terms) is really simple, located in /models/quests.js:</p>
<pre><code class="lang-language">const questSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    }
});
</code></pre>
<p>The above indicates that the database should store our two fields: a quest name and a quest description.  Both of these fields are strings, and required. All GET and POST requests will have to conform to this model to interact with the database.</p>
<p>After wiring all of this up and POSTing a few new quests, the front end site started populating with data:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/Vue-Front-End-1.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The process of setting up the Express API was not without its hair pulling, however. Being a primarily front end and 2D game developer, I've become intimately familiar with how dispersed the JavaScript ecosystem can feel. This frustration was magnified in attempting to build a back end app. There are a <em>lot</em> of packages required to get everything up and running, each of which having its own required configuration and implementation.</p>
<p>If you're looking for a framework that just does everything out of the box, Express is most certainly not the choice for you. It's lightweight, flexible, and easy to read, in a very "choose-your-own-adventure" fashion. I quite like how clean the code is and the ability to structure my projects as I see fit, but troubleshooting and error handling do leave a lot to be desired.</p>
<h3 id="heading-javascriptexpress-resources">JavaScript/Express Resources</h3>
<p>To build the JavaScript API, I used:</p>
<ul>
<li><a target="_blank" href="https://nodejs.org/en/">NodeJS</a>/<a target="_blank" href="https://www.npmjs.com/">NPM</a> for package management</li>
<li><a target="_blank" href="https://expressjs.com/">Express</a> as the main web framework</li>
<li><a target="_blank" href="https://www.npmjs.com/package/dotenv">Dotenv</a> to create environment-specific variables</li>
<li><a target="_blank" href="https://nodemon.io/">Nodemon</a> to watch files for changes and restart the server so I didn't have to</li>
<li><a target="_blank" href="https://expressjs.com/en/resources/middleware/cors.html">CORS</a> to allow for <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">cross-origin requests</a> (basically a pain if you're trying to make requests from a client to a server that are both running locally on your machine)</li>
<li><a target="_blank" href="https://www.mongodb.com/">MongoDB</a> for the <a target="_blank" href="https://www.mongodb.com/nosql-explained">NoSQL</a> database</li>
<li><a target="_blank" href="https://mongoosejs.com/">Mongoose</a> for writing models that map onto MongoDB </li>
<li><a target="_blank" href="https://dev.to/beznet/build-a-rest-api-with-node-express-mongodb-4ho4">This API tutorial</a> to provide a basic understanding of how to create an Express-MongoDB stack</li>
<li><a target="_blank" href="https://medium.com/@anaida07/mevn-stack-application-part-1-3a27b61dcae0">This MEVN tutorial</a> to fill in the gaps of running a MongoDB-Express-Vue-Node full stack</li>
</ul>
<h2 id="heading-python-api-flask">Python API: Flask</h2>
<p>In the process of building the Express API, I had a conversation with a data science friend who works in Python. This gave me the idea of trying out non-JavaScript frameworks to see if they were better suited for my app.</p>
<p>I took a cursory look at <a target="_blank" href="https://www.djangoproject.com/">Django</a>, since I'd been hearing about it as a powerhouse back end framework that provides everything out of the box. I was a little intimidated by how opinionated it seemed, and opted to try out <a target="_blank" href="https://palletsprojects.com/p/flask/">Flask</a> instead, which kind of felt like the Python equivalent of Express.</p>
<p>I followed the first few bits of the excellent <a target="_blank" href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world">Flask Mega-Tutorial</a> to get my app structure set up, using the companion <a target="_blank" href="https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask">RESTful API tutorial</a> to fill in the pieces of HTTP requests. The file structure turned out to be only a shade more complex than that of the Express API:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/Flask-Structure.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The tutorial I followed uses <a target="_blank" href="https://www.sqlite.org/index.html">SQLite</a> for its database, with <a target="_blank" href="https://flask-sqlalchemy.palletsprojects.com/en/2.x/">Flask-SQLAlchemy</a> as an <a target="_blank" href="https://en.wikipedia.org/wiki/Object-relational_mapping">ORM</a>. The HTTP request code that's most analogous to the Express API is located in /app/routes.py:</p>
<pre><code class="lang-python"><span class="hljs-meta">@app.route('/quests', methods=['GET'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get_quests</span>():</span>
    questQuery = Quest.query.all()
    quests = {}
    <span class="hljs-keyword">for</span> quest <span class="hljs-keyword">in</span> questQuery:
        quests[quest.name] = quest.description
    <span class="hljs-keyword">return</span> jsonify(quests)

<span class="hljs-meta">@app.route('/quests', methods=['POST'])</span>
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">post_quest</span>():</span>
    newQuest = Quest(name=request.json[<span class="hljs-string">'name'</span>], description=request.json[<span class="hljs-string">'description'</span>])
    db.session.add(newQuest)
    db.session.commit()
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Quest Added!"</span>
</code></pre>
<p>Similarly, the database model (akin to the Mongoose "schema") is in /app/models.py:</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Quest</span>(<span class="hljs-params">db.Model</span>):</span>
    name = db.Column(db.String(<span class="hljs-number">256</span>), primary_key=<span class="hljs-literal">True</span>, index=<span class="hljs-literal">True</span>, unique=<span class="hljs-literal">True</span>)
    description = db.Column(db.String(<span class="hljs-number">256</span>), index=<span class="hljs-literal">True</span>, unique=<span class="hljs-literal">True</span>)
</code></pre>
<p>As I mentioned, I'm more familiar with JavaScript and C# than with Python, and working with the latter to build the Flask API felt like cheating. Certain things like pathing, package handling, and writing workable code were just <em>easy</em>, although I did get hung up on getting the API to correctly parse JSON for the client. I suspect that was more of an issue of my unfamiliarity with the language than anything else, but it did take time to troubleshoot.</p>
<p>To be quite honest, coming from a non-Flask background, I did kind of expect to complete a couple of tutorials and spin up an API without having to do all that much work for it.  </p>
<p>I can't say that it turned out that way, as Python does have its own particulars that require some time to get used to. Still, the Python ecosystem appears to be extremely well organized, and I enjoyed my time building the Flask API.</p>
<p>I've also heard that Django is a better and more scalable option for larger projects. But it seems like it would involve a separate, and steeper, learning curve to become proficient. </p>
<p>Flask was easy enough for me as a non-Python developer to pick up and build something over a weekend. I suspect that learning Django would take quite a bit longer, but with potentially greater dividends over the long run. </p>
<h3 id="heading-pythonflask-resources">Python/Flask Resources</h3>
<p>To build the Flask API, I used:</p>
<ul>
<li><a target="_blank" href="https://www.python.org/">Python 3</a>/<a target="_blank" href="https://pip.pypa.io/en/stable/">pip</a> for package management</li>
<li><a target="_blank" href="https://palletsprojects.com/p/flask/">Flask</a> as the main web framework</li>
<li><a target="_blank" href="https://pypi.org/project/python-dotenv/">python-dotenv</a> to configure environment variables</li>
<li><a target="_blank" href="https://www.sqlite.org/index.html">SQLite</a> as the database </li>
<li><a target="_blank" href="https://flask-sqlalchemy.palletsprojects.com/en/2.x/">Flask-SQLAlchemy</a> as the ORM to work with SQLite</li>
<li><a target="_blank" href="https://flask-migrate.readthedocs.io/en/latest/">Flask-Migrate</a> as an additional tool to migrate data to SQLite </li>
<li><a target="_blank" href="https://flask-cors.readthedocs.io/en/latest/">Flask-CORS</a> to handle the same CORS issue as with the Express API</li>
<li>The <a target="_blank" href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world">Flask Mega-Tutorial</a> to learn the basics</li>
<li>The <a target="_blank" href="https://blog.miguelgrinberg.com/post/designing-a-restful-api-with-python-and-flask">Flask REST API tutorial</a> to understand how to receive HTTP requests</li>
</ul>
<h2 id="heading-c-api-aspnet">C# API: ASP.NET</h2>
<p>I can't tell you how many times I've Googled ".<a target="_blank" href="https://dotnet.microsoft.com/">NET</a>" to understand what it is, how it's different from <a target="_blank" href="https://dotnet.microsoft.com/apps/aspnet">ASP.NET</a>, and why I'd want to use any of it. My C# knowledge comes mainly from working with <a target="_blank" href="https://unity.com/">Unity</a>, which exists somewhat adjacent to .NET and doesn't provide for a lot of exposure to Microsoft's larger ecosystem.</p>
<p>I've spent some time researching <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/razor-pages/?view=aspnetcore-3.1&amp;tabs=visual-studio">Razor Pages</a> and <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/mvc/overview?view=aspnetcore-3.1">MVC</a>, and finally came to understand ASP.NET's breadth of features as Microsoft's open source web framework. I decided to toss ASP.NET into the hat for a potential back end for my app, and set about working through the <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-3.1&amp;tabs=visual-studio">official web API tutorial</a> with ASP.NET Core and MongoDB.</p>
<p>The file structure for this version of the API was more complex than the others, given that .NET projects tend to have a much larger footprint:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2020/02/ASPNet-Structure.PNG" alt="Image" width="600" height="400" loading="lazy"></p>
<p>I should also mention that I already had Visual Studio and all of the required workloads installed, which made the setup process easier. Plus, having spent time with MongoDB for the Express API, I found the database portion of the project to be similar, although by default, ASP.NET seems to prefer using Microsoft's <a target="_blank" href="https://www.microsoft.com/en-us/sql-server/default.aspx">SQL Server</a> and the <a target="_blank" href="https://docs.microsoft.com/en-us/ef/">Entity Framework ORM</a>.</p>
<p>The ASP.NET code for HTTP requests is a bit more complex than what we've seen with the two other APIs, but it's no match for all of the code that sits <em>around</em> it.  </p>
<p>First, consider this snippet in /Controllers/QuestController.cs that handles requests:</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">QuestAPI.Controllers</span>
{
    [<span class="hljs-meta">Route(<span class="hljs-meta-string">"quests/"</span>)</span>]
    [<span class="hljs-meta">ApiController</span>]
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">QuestsController</span> : <span class="hljs-title">ControllerBase</span>
    {
        <span class="hljs-keyword">private</span> <span class="hljs-keyword">readonly</span> QuestService _questService;

        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">QuestsController</span>(<span class="hljs-params">QuestService questService</span>)</span>
        {
            _questService = questService;
        }

        [<span class="hljs-meta">HttpGet</span>]
        <span class="hljs-keyword">public</span> ActionResult&lt;List&lt;Quest&gt;&gt; Get() =&gt;
            _questService.Get();

        [<span class="hljs-meta">HttpPost</span>]
        <span class="hljs-function"><span class="hljs-keyword">public</span> ActionResult&lt;Quest&gt; <span class="hljs-title">Create</span>(<span class="hljs-params">Quest quest</span>)</span>
        {
            _questService.Create(quest);
            <span class="hljs-keyword">return</span> CreatedAtRoute(<span class="hljs-string">"GetQuest"</span>, <span class="hljs-keyword">new</span> { id = quest.Id.ToString() }, quest);
        }
    }
}
</code></pre>
<p>Not too terrible, almost kind of readable, in a C# sort of way. The data model in /Models/Quest.cs is even easier:</p>
<pre><code class="lang-c#"><span class="hljs-keyword">namespace</span> <span class="hljs-title">QuestAPI.Models</span>{
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Quest</span>
    {
        [<span class="hljs-meta">BsonId</span>]
        [<span class="hljs-meta">BsonRepresentation(BsonType.ObjectId)</span>]
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Id { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }

        [<span class="hljs-meta">BsonElement(<span class="hljs-meta-string">"Name"</span>)</span>]
        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Name { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }

        <span class="hljs-keyword">public</span> <span class="hljs-keyword">string</span> Description { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">set</span>; }
    }
}
</code></pre>
<p>These two snippets essentially do the same things as the previous examples that we've seen: take requests from the front end, process them to get or modify data in the database, and send a response back to the client.  </p>
<p>Yet, as you can probably tell from the complex file structure, there's so much code that surrounds these snippets, along with <a target="_blank" href="https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/">Interfaces</a>, <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1">Dependency Injection</a>, and other abstractions, that it can be challenging to understand how it all works together.</p>
<p>Consider the following configuration code in /Startup.cs:</p>
<pre><code class="lang-c#">        <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">ConfigureServices</span>(<span class="hljs-params">IServiceCollection services</span>)</span>
        {
            services.Configure&lt;QuestDatabaseSettings&gt;(Configuration.GetSection(<span class="hljs-keyword">nameof</span>(QuestDatabaseSettings)));

            services.AddSingleton&lt;IQuestDatabaseSettings&gt;(sp =&gt; sp.GetRequiredService&lt;IOptions&lt;QuestDatabaseSettings&gt;&gt;().Value);

            services.AddSingleton&lt;QuestService&gt;();

            services.AddCors(options =&gt;
            {
                options.AddPolicy(MyAllowSpecificOrigins, builder =&gt;
                {
                    builder.WithOrigins(<span class="hljs-string">"http://localhost:3000/quests"</span>, <span class="hljs-string">"http://localhost:8080"</span>).AllowAnyHeader().AllowAnyMethod();
                });
            });

            services.AddControllers();
        }
</code></pre>
<p>Or this particularly nested bit from a separate <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-3.1&amp;tabs=visual-studio">SQL Server web API tutorial</a>:</p>
<pre><code class="lang-c#">    [<span class="hljs-meta">HttpGet</span>]
    <span class="hljs-keyword">public</span> <span class="hljs-keyword">async</span> Task&lt;ActionResult&lt;IEnumerable&lt;TodoItemDTO&gt;&gt;&gt; GetTodoItems()
    {
        <span class="hljs-keyword">return</span> <span class="hljs-keyword">await</span> _context.TodoItems
            .Select(x =&gt; ItemToDTO(x))
            .ToListAsync();
    }
</code></pre>
<p>Lol. What?? As a new user, even familiar as I am with C#, I can go line-by-line to understand each abstraction, or I can just trust that the framework is handling everything for me and forget about it.</p>
<p>I tend to want to know exactly how my code works so that I can fix or alter it if necessary. But I certainly feel like my time spent learning the ins-and-outs of ASP.NET could be better utilized towards mastering another framework.</p>
<p>To be fair, ASP.NET appears to be similar to Django in being more opinionated and providing you with a ton of stuff out of the box, including an authentication solution, database management, and the lot. If these things are important to you, it's certainly worth considering.  </p>
<p>It also has the full support of Microsoft and an open source community. So if you're looking at developing enterprise-level applications that need to scale, you might want to take a longer look at ASP.NET as a potential solution.</p>
<h3 id="heading-caspnet-resources">C#/ASP.Net Resources</h3>
<p>To build the ASP.Net API, I used the following resources:</p>
<ul>
<li><a target="_blank" href="https://visualstudio.microsoft.com/downloads/">Visual Studio Community</a> as my code editor and IDE, with the ASP.NET and web development workload installed (I already had MongoDB running from the Express API)</li>
<li>Microsoft's <a target="_blank" href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mongo-app?view=aspnetcore-3.1&amp;tabs=visual-studio">official tutorial</a> for building web APIs with ASP.NET and MongoDB</li>
</ul>
<h2 id="heading-tldr">TL;DR</h2>
<p>In all, with some slight variations and hiccups among them, I've gotten each of the web APIs to work with the Vue client, with the ability to view quests from and add quests to the database. Hopefully, my explanation of the process has been helpful in your own search for a back end framework, but here are some additional recommendations just in case:</p>
<ul>
<li>If you're a JavaScript developer and/or want to manage everything that your application does, including its architecture, consider using Express.</li>
<li>If you're a Python developer and/or want a pleasant experience in developing small projects, try Flask, but consider using Django if you need more out-of-the-box support and don't mind conforming to an opinionated framework.</li>
<li>If you're a C# developer and willing to spend the time to learn the most arcane details of C# coding best practices, consider using ASP.NET. Alternatively, if you need enterprise-level support right out of the box, you'd be hard-pressed to find better.</li>
<li>If you don't know what to use and just want to learn back end development, take a look at Flask.  It's easy to work with and will teach you the basics that you'll need to know for building web apps in any coding language.</li>
<li>If you don't know what to use and want an adventure, choose Express. There's a rabbit hole of package management and Stack Overflow questions waiting that may make you tear your hair out, but you'll learn a lot about the JavaScript ecosystem and web development in general.</li>
</ul>
<p>Additionally, two things bear mentioning that threw me for a spin in this process: CORS and environment variables. The former I've mentioned in this article a couple of times already, but it's worth discussing again to understand the scope of building a full stack app on your machine.</p>
<p>Unless you have an integrated development environment that's handling the whole stack for you, you'll likely have a client, a server, and a database that are all running independently of one another.  </p>
<p>In the Express API section above, for example, I was running </p>
<ol>
<li>the Vue CLI server, which rendered my front end app on port 8080; </li>
<li>an NPM script to spin up the Express API server on port 3000; and</li>
<li>a separate instance of the Mongo database to get everything working together. That's three command prompts open and a general mess!</li>
</ol>
<p>If you dig into the Vue code above (or on GitHub), you'll see that the requests made on behalf of the client, running on http://localhost:8080, are to the server on http://localhost:3000, which is where the Express API is listening. This is called "cross-origin resource sharing," or <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">CORS</a>, and it's blocked by the browser for security concerns. Most frameworks require you to install an additional package to get the whole thing running in your local environment.</p>
<p>Second, you'll want to become comfortable with <a target="_blank" href="https://en.wikipedia.org/wiki/Environment_variable">environment variables</a>, which can really help smooth some rough pathing edges at runtime. I used <a target="_blank" href="https://www.npmjs.com/package/dotenv">dotenv</a> and <a target="_blank" href="https://pypi.org/project/Flask-Env/">Flask-Env</a> for the Express and Flask projects, respectively. </p>
<p>Both packages allow you to configure things like where your database lives, or what default port your application should be using, in one document. Your application then uses that document at runtime to figure out where to find everything, without any further configuration needed from you.</p>
<p>One final note that may be helpful if you're just working on a back end project and don't want to go through the trouble of building a front end client: consider using a third-party app like <a target="_blank" href="https://www.postman.com/">Postman</a>. I used it to make HTTP requests to each of the APIs to make sure they were working properly before layering on the Vue client and trying to get the whole stack running altogether. </p>
<p>I hope this article has been helpful for you in your own process of looking for a back end framework.  Let me know what you find! </p>
<p>If you enjoyed this article, please consider <a target="_blank" href="https://www.nightpathpub.com/">checking out my games and books</a>, <a target="_blank" href="https://www.youtube.com/msfarzan?sub_confirmation=1">subscribing to my YouTube channel</a>, or <a target="_blank" href="https://discord.gg/RF6k3nB">joining the <em>Entromancy</em> Discord</a>.</p>
<p>M. S. Farzan, Ph.D. has written and worked for high-profile video game companies and editorial websites such as Electronic Arts, Perfect World Entertainment, Modus Games, and MMORPG.com, and has served as the Community Manager for games like <em>Dungeons &amp; Dragons Neverwinter</em> and <em>Mass Effect: Andromeda</em>. He is the Creative Director and Lead Game Designer of <em><a target="_blank" href="https://www.nightpathpub.com/rpg">Entromancy: A Cyberpunk Fantasy RPG</a></em> and author of <em><a target="_blank" href="http://nightpathpub.com/books">The Nightpath Trilogy</a></em>. Find M. S. Farzan on Twitter <a target="_blank" href="https://twitter.com/sominator">@sominator</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Learn how to build web apps using ASP.NET Core 3.1 ]]>
                </title>
                <description>
                    <![CDATA[ ASP.NET Core is an open source web-development framework for building web apps on the .NET platform. While originally only for Windows, it is now available on macOS and Linux as well. We've released a 3 hour course on the freeCodeCamp.org YouTube cha... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/asp-net-core-3-1-course/</link>
                <guid isPermaLink="false">66b20093276d158502db2eaf</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ youtube ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Beau Carnes ]]>
                </dc:creator>
                <pubDate>Wed, 05 Feb 2020 17:46:38 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/02/asp.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>ASP.NET Core is an open source web-development framework for building web apps on the .NET platform. While originally only for Windows, it is now available on macOS and Linux as well.</p>
<p>We've released a 3 hour course on the freeCodeCamp.org YouTube channel so you can learn ASP.NET Core 3.1. The course is for beginners, but it is expected that you have some knowledge of C#. (If you want to learn C#, check out <a target="_blank" href="https://www.youtube.com/watch?v=GhQdlIFylQ8">this great C# course</a>.)</p>
<p>In this course you will first learn about the history and basics of ASP.NET Core. Then you will learn how to build a small book list application with CRUD operations. You will learn how to use the Entity Framework for integration with a database with ASP.NET Core Razor Pages.</p>
<p>You will also learn how to build a book list application using ASP.NET MVC and see how to use DataTables with API Calls in a Razor Project.</p>
<p>This course was developed by Bhrugen Patel. Watch it below or <a target="_blank" href="https://www.youtube.com/watch?v=C5cnZ-gZy2I">on the freeCodeCamp.org YouTube channel</a>.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/C5cnZ-gZy2I" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to easily implement QRCoder in ASP.NET Core using C# ]]>
                </title>
                <description>
                    <![CDATA[ By Yogi QRCoder is a very popular QR Code implementation library written in C#. It is available in GitHub. Here I am going to implement the QRCoder library to generate QR Codes in my ASP.NET Core application. I will also be using C#. I will implement... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-easily-implement-qrcoder-in-asp-net-core-using-c-10c4aa857e84/</link>
                <guid isPermaLink="false">66c351d8bc39b1419091be3a</guid>
                
                    <category>
                        <![CDATA[ Aspnetcore ]]>
                    </category>
                
                    <category>
                        <![CDATA[ C# ]]>
                    </category>
                
                    <category>
                        <![CDATA[ General Programming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ tech  ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Thu, 23 May 2019 15:58:13 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*qL5RAfsdeIw875myQ3f9Ag.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Yogi</p>
<p>QRCoder is a very popular QR Code implementation library written in C#. It is available in <a target="_blank" href="https://github.com/codebude/QRCoder"><strong>GitHub</strong></a><strong>.</strong> Here I am going to implement the QRCoder library to generate QR Codes in my ASP.NET Core application. I will also be using C#.</p>
<p>I will implement QRCoder in 3 ways, which are:</p>
<ol>
<li><p>Create QR Code Bitmap image for any text.</p>
</li>
<li><p>Create QR Code File (.qrr) for any text and then save these files in the application.</p>
</li>
<li><p>Read and display all the QR Code Files (.qrr).</p>
</li>
</ol>
<p>Let’s start with the Installation of QRCoder in <a target="_blank" href="https://www.yogihosting.com/category/aspnet-core/"><strong>ASP.NET Core</strong></a> Framework.</p>
<p><a target="_blank" href="https://github.com/yogyogi/QRCoder-implemented-in-ASP.NET-Core"><strong>You can download the full code from my GitHub Respositiory</strong></a><strong>.</strong></p>
<h3 id="heading-installation"><strong>Installation</strong></h3>
<p>Install QRCoder via NuGet Package Manager. If you want to use NuGet, just search for “QRCoder” or run the following command in the NuGet Package Manager console:</p>
<p><code>PM&gt; Install-Package QRCoder</code></p>
<p>The QRCoder will install in 1 minute and will be ready to use.</p>
<p>Now let us start with the implementation of QRCoder in the 3 ways mentioned above.</p>
<h3 id="heading-create-qr-code-bitmap-image-for-any-text"><strong>Create QR Code Bitmap image for any text</strong></h3>
<p>Create a new Controller called ‘<code>QRCoderController</code>’ inside the Controllers folder. The controller will be created and it will have just one Action Method called ‘<code>Index</code>’ in it:</p>
<pre><code>public IActionResult Index()
{
    <span class="hljs-keyword">return</span> View();
}
</code></pre><p>Import the following namespaces in the controller:</p>
<pre><code>using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using QRCoder;
</code></pre><p>Next, add the Index Action of type <code>[HttpPost]</code> to the controller:</p>
<pre><code>[HttpPost]
public IActionResult Index(string qrText)
{
    QRCodeGenerator qrGenerator = <span class="hljs-keyword">new</span> QRCodeGenerator();
    QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText,
    QRCodeGenerator.ECCLevel.Q);
    QRCode qrCode = <span class="hljs-keyword">new</span> QRCode(qrCodeData);
    Bitmap qrCodeImage = qrCode.GetGraphic(<span class="hljs-number">20</span>);
    <span class="hljs-keyword">return</span> View(BitmapToBytes(qrCodeImage));
}
</code></pre><blockquote>
<p>This Index Action receives a string parameter called ‘qrText’. It contains the text that is provided by an Input control defined in the View. This text will be converted to QR Code Bitmap image. The following code lines do this work:</p>
</blockquote>
<pre><code>QRCodeGenerator qrGenerator = <span class="hljs-keyword">new</span> QRCodeGenerator();

QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);

QRCode qrCode = <span class="hljs-keyword">new</span> QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(<span class="hljs-number">20</span>);
</code></pre><p>The QRCode object (‘<code>qrCode</code>’) defined calls a static function called ‘<code>BitmapToBytes()</code>’. The role of this function is to convert the Bitmap image to ‘<code>Byte[]</code>’.</p>
<p>Add this function to your controller:</p>
<pre><code>private <span class="hljs-keyword">static</span> Byte[] BitmapToBytes(Bitmap img)
{
    using (MemoryStream stream = <span class="hljs-keyword">new</span> MemoryStream())
    {
        img.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
        <span class="hljs-keyword">return</span> stream.ToArray();
    }
}
</code></pre><p>Finally create the Index View inside the ‘<code>Views/QRCoder</code>’ folder with the following code:</p>
<pre><code>@model Byte[]
@{
    Layout = <span class="hljs-literal">null</span>;
}

&lt;!DOCTYPE html&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Implementing QRCoder in ASP.NET Core - Create QR Code<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-tag">body</span> {
      <span class="hljs-attribute">background</span>: <span class="hljs-number">#111</span> no-repeat;
      <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">-webkit-gradient</span>(radial, <span class="hljs-number">50%</span> <span class="hljs-number">0</span>, <span class="hljs-number">150</span>, <span class="hljs-number">50%</span> <span class="hljs-number">0</span>, <span class="hljs-number">300</span>, from(#<span class="hljs-number">444</span>), <span class="hljs-built_in">to</span>(#<span class="hljs-number">111</span>));
    }

    <span class="hljs-selector-tag">h1</span>,
    <span class="hljs-selector-tag">h2</span>,
    <span class="hljs-selector-tag">h3</span> {
      <span class="hljs-attribute">text-align</span>: center;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span> <span class="hljs-number">0</span>;
    }

    <span class="hljs-selector-tag">h1</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
    }

    <span class="hljs-selector-tag">h2</span> <span class="hljs-selector-tag">a</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#0184e3</span>;
      <span class="hljs-attribute">text-decoration</span>: none;
    }

    <span class="hljs-selector-tag">h3</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">23px</span>;
      <span class="hljs-attribute">border-bottom</span>: solid <span class="hljs-number">3px</span> <span class="hljs-number">#CCC</span>;
      <span class="hljs-attribute">padding-bottom</span>: <span class="hljs-number">10px</span>;
    }

    <span class="hljs-selector-tag">h3</span> <span class="hljs-selector-tag">a</span> {
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#00e8ff</span>;
      <span class="hljs-attribute">text-decoration</span>: none;
    }

    <span class="hljs-selector-tag">h3</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span>,
    <span class="hljs-selector-tag">h2</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
      <span class="hljs-attribute">text-decoration</span>: underline;
    }

    <span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
      <span class="hljs-attribute">margin</span>: auto;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
    }

    <span class="hljs-selector-class">.container</span> <span class="hljs-selector-id">#content</span> {
      <span class="hljs-attribute">border</span>: dashed <span class="hljs-number">2px</span> <span class="hljs-number">#CCC</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
    }

    <span class="hljs-selector-id">#reset</span> {
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span> <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">background</span>: <span class="hljs-number">#4CAF50</span>;
      <span class="hljs-attribute">border</span>: none;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
      <span class="hljs-attribute">cursor</span>: pointer;
    }

    <span class="hljs-selector-id">#reset</span><span class="hljs-selector-pseudo">:hover</span> {
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#4CAF50</span>;
      <span class="hljs-attribute">background</span>: <span class="hljs-number">#FFF</span>;
    }

    <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    }

    <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> {
      <span class="hljs-attribute">height</span>: <span class="hljs-number">80px</span>;
      <span class="hljs-attribute">background</span>: darkcyan;
    }

    <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> <span class="hljs-selector-tag">td</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
      <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">5px</span>;
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"content"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Implementing QRCoder in ASP.NET Core - Create QR Code<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"http://www.yogihosting.com/category/aspnet-core/"</span>&gt;</span>Read the tutorial on YogiHosting » <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"reset"</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"location=''"</span>&gt;</span>Reset »<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"viewContent"</span>&gt;</span>
        @using (Html.BeginForm(null, null, FormMethod.Post)) {
        <span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">label</span>&gt;</span>Enter text for creating QR Code<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>
                &lt;/<span class="hljs-attr">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"qrText"</span> /&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">"2"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
        }
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      @{
        if (Model != null)
        {
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>QR Code Successfully Generated<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"@String.Format("</span><span class="hljs-attr">data:image</span>/<span class="hljs-attr">png</span>;<span class="hljs-attr">base64</span>,{<span class="hljs-attr">0</span>}", <span class="hljs-attr">Convert.ToBase64String</span>(<span class="hljs-attr">Model</span>))" /&gt;</span>
        }
      }
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></span>
</code></pre><p>The Index View has an ‘<code>input</code>’ control. The user enters their text into this control to create the QR Code:</p>
<p><code>&lt;input type="text" name="qrText"</code> /&gt;</p>
<p>Once the QR Code is generated by the Index Action method, its ‘<code>byte</code>’ array is returned to the View as model and then the bitmap image is displayed by the below code:</p>
<pre><code>@{
  <span class="hljs-keyword">if</span> (Model != <span class="hljs-literal">null</span>)
  {
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>QR Code Successfully Generated<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span></span>
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"@String.Format("</span><span class="hljs-attr">data:image</span>/<span class="hljs-attr">png</span>;<span class="hljs-attr">base64</span>,{<span class="hljs-attr">0</span>}", <span class="hljs-attr">Convert.ToBase64String</span>(<span class="hljs-attr">Model</span>))" /&gt;</span></span>
  }
}
</code></pre><h4 id="heading-testing-the-code"><strong>Testing the Code</strong></h4>
<p>Run your application and go to the URL — ‘<code>http://localhost:50755/QRCoder</code>’ to invoke the Index Action method.</p>
<p>In the text box, add your text and click the submit button to create the QR Code Bitmap image.</p>
<p>See this image which illustrates it working:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/RZJScQFTxL1upNaGcmdXmOWaJR3u10Zq1RjJ" alt="Image" width="600" height="460" loading="lazy">
<em><strong>create QRCode Bitmap Image</strong></em></p>
<h3 id="heading-create-qr-code-file-qrr-for-any-text-and-then-save-these-files-in-the-application"><strong>Create QR Code File (.qrr) for any text and then save these files in the application</strong></h3>
<p>You can also generate QR Code files for a text and save it in your website. These files have .<em>qrr</em> extension.</p>
<p>To your controller add the following Action methods called ‘<code>GenerateFile</code>’:</p>
<pre><code>public IActionResult GenerateFile()
{
  <span class="hljs-keyword">return</span> View();
}

[HttpPost]
public IActionResult GenerateFile(string qrText)
{
  QRCodeGenerator qrGenerator = <span class="hljs-keyword">new</span> QRCodeGenerator();
  QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText,   QRCodeGenerator.ECCLevel.Q);

  string fileGuid = Guid.NewGuid().ToString().Substring(<span class="hljs-number">0</span>, <span class="hljs-number">4</span>);

  qrCodeData.SaveRawData(<span class="hljs-string">"wwwroot/qrr/file-"</span> + fileGuid + <span class="hljs-string">".qrr"</span>, QRCodeData.Compression.Uncompressed);

  QRCodeData qrCodeData1 = <span class="hljs-keyword">new</span> QRCodeData(<span class="hljs-string">"wwwroot/qrr/file-"</span> + fileGuid + <span class="hljs-string">".qrr"</span>, QRCodeData.Compression.Uncompressed);

  QRCode qrCode = <span class="hljs-keyword">new</span> QRCode(qrCodeData1);
  Bitmap qrCodeImage = qrCode.GetGraphic(<span class="hljs-number">20</span>);
  <span class="hljs-keyword">return</span> View(BitmapToBytes(qrCodeImage));
}
</code></pre><p>The <code>[HttpPost]</code> version of this action method generates the QR Code files inside the ‘<code>wwwroot/qrr</code>’ folder. The code that does this work is the following:</p>
<pre><code>QRCodeGenerator qrGenerator = <span class="hljs-keyword">new</span> QRCodeGenerator();

QRCodeData qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);

string fileGuid = Guid.NewGuid().ToString().Substring(<span class="hljs-number">0</span>, <span class="hljs-number">4</span>);

qrCodeData.SaveRawData(<span class="hljs-string">"wwwroot/qrr/file-"</span> + fileGuid + <span class="hljs-string">".qrr"</span>, QRCodeData.Compression.Uncompressed);
</code></pre><p>Once the .qrr file is created then I am simply reading it for its saved location in the website. Then I am converting it to Bitmap type and finally sending the image’s bytes to the view. The corresponding code is:</p>
<pre><code>QRCodeData qrCodeData1 = <span class="hljs-keyword">new</span> QRCodeData(<span class="hljs-string">"wwwroot/qrr/file-"</span> + fileGuid + <span class="hljs-string">".qrr"</span>, QRCodeData.Compression.Uncompressed);

QRCode qrCode = <span class="hljs-keyword">new</span> QRCode(qrCodeData1);
Bitmap qrCodeImage = qrCode.GetGraphic(<span class="hljs-number">20</span>);

<span class="hljs-keyword">return</span> View(BitmapToBytes(qrCodeImage));
</code></pre><p>Next, add the GenerateFile view inside the ‘<code>Views/QRCoder</code>’ folder and add the following code to it:</p>
<pre><code>@model Byte[]
@{
    Layout = <span class="hljs-literal">null</span>;
}

&lt;!DOCTYPE html&gt;
<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width"</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Implementing QRCoder in ASP.NET Core - Create QR Code File<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
    <span class="hljs-selector-tag">body</span> {
      <span class="hljs-attribute">background</span>: <span class="hljs-number">#111</span> no-repeat;
      <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">-webkit-gradient</span>(radial, <span class="hljs-number">50%</span> <span class="hljs-number">0</span>, <span class="hljs-number">150</span>, <span class="hljs-number">50%</span> <span class="hljs-number">0</span>, <span class="hljs-number">300</span>, from(#<span class="hljs-number">444</span>), <span class="hljs-built_in">to</span>(#<span class="hljs-number">111</span>));
    }

    <span class="hljs-selector-tag">h1</span>,
    <span class="hljs-selector-tag">h2</span>,
    <span class="hljs-selector-tag">h3</span> {
      <span class="hljs-attribute">text-align</span>: center;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
      <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span> <span class="hljs-number">0</span>;
    }

    <span class="hljs-selector-tag">h1</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
    }

    <span class="hljs-selector-tag">h2</span> <span class="hljs-selector-tag">a</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#0184e3</span>;
      <span class="hljs-attribute">text-decoration</span>: none;
    }

    <span class="hljs-selector-tag">h3</span> {
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">23px</span>;
      <span class="hljs-attribute">border-bottom</span>: solid <span class="hljs-number">3px</span> <span class="hljs-number">#CCC</span>;
      <span class="hljs-attribute">padding-bottom</span>: <span class="hljs-number">10px</span>;
    }

    <span class="hljs-selector-tag">h3</span> <span class="hljs-selector-tag">a</span> {
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#00e8ff</span>;
      <span class="hljs-attribute">text-decoration</span>: none;
    }

    <span class="hljs-selector-tag">h3</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span>,
    <span class="hljs-selector-tag">h2</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
      <span class="hljs-attribute">text-decoration</span>: underline;
    }

    <span class="hljs-selector-class">.container</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
      <span class="hljs-attribute">margin</span>: auto;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
      <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
    }

    <span class="hljs-selector-class">.container</span> <span class="hljs-selector-id">#content</span> {
      <span class="hljs-attribute">border</span>: dashed <span class="hljs-number">2px</span> <span class="hljs-number">#CCC</span>;
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
    }

    <span class="hljs-selector-id">#reset</span> {
      <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span> <span class="hljs-number">10px</span>;
      <span class="hljs-attribute">background</span>: <span class="hljs-number">#4CAF50</span>;
      <span class="hljs-attribute">border</span>: none;
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
      <span class="hljs-attribute">cursor</span>: pointer;
    }

    <span class="hljs-selector-id">#reset</span><span class="hljs-selector-pseudo">:hover</span> {
      <span class="hljs-attribute">color</span>: <span class="hljs-number">#4CAF50</span>;
      <span class="hljs-attribute">background</span>: <span class="hljs-number">#FFF</span>;
    }

    <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    }

    <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> {
      <span class="hljs-attribute">height</span>: <span class="hljs-number">80px</span>;
      <span class="hljs-attribute">background</span>: darkcyan;
    }

    <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> <span class="hljs-selector-tag">td</span> {
      <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
      <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">5px</span>;
    }
  </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"content"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Implementing QRCoder in ASP.NET Core - Create QR Code File<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"http://www.yogihosting.com/category/aspnet-core/"</span>&gt;</span>Read the tutorial on YogiHosting » <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"reset"</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"location=''"</span>&gt;</span>Reset »<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"viewContent"</span>&gt;</span>
        @using (Html.BeginForm(null, null, FormMethod.Post)) {
        <span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">label</span>&gt;</span>Enter text for creating QR File<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"qrText"</span> /&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">td</span> <span class="hljs-attr">colspan</span>=<span class="hljs-string">"2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
        }
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      @{ if (Model != null) {
      <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>QR Code file Successfully Generated<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"@String.Format("</span> <span class="hljs-attr">data:image</span>/<span class="hljs-attr">png</span>;<span class="hljs-attr">base64</span>,{<span class="hljs-attr">0</span>} ", <span class="hljs-attr">Convert.ToBase64String</span>(<span class="hljs-attr">Model</span>))" /&gt;</span> } }
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></span>
</code></pre><p>The code of this View is exactly similar to the ‘Index’ View and works exactly like it.</p>
<p>The URL to invoke this View is ‘<code>http://localhost:50755/QRCoder/GenerateFile</code>’.</p>
<h3 id="heading-read-and-display-all-the-qr-code-files-qrr"><strong>Read and display all the QR Code Files (.qrr)</strong></h3>
<p>You can also read all the .qrr files saved in the website. Go to your controller and add a new action called ‘ViewFile’:</p>
<pre><code>public IActionResult ViewFile()
{
  List&lt;KeyValuePair&lt;string, Byte[]&gt;&gt; fileData=<span class="hljs-keyword">new</span> List&lt;KeyValuePair&lt;string, byte[]&gt;&gt;();

  KeyValuePair&lt;string, Byte[]&gt; data;
  string[] files = Directory.GetFiles(<span class="hljs-string">"wwwroot/qrr"</span>);
  foreach (string file <span class="hljs-keyword">in</span> files)
  {
    QRCodeData qrCodeData = <span class="hljs-keyword">new</span> QRCodeData(file, QRCodeData.Compression.Uncompressed);

    QRCode qrCode = <span class="hljs-keyword">new</span> QRCode(qrCodeData);
    Bitmap qrCodeImage = qrCode.GetGraphic(<span class="hljs-number">20</span>);

    Byte[] byteData = BitmapToBytes(qrCodeImage);
    data = <span class="hljs-keyword">new</span> KeyValuePair&lt;string, Byte[]&gt;(Path.GetFileName(file), byteData);
    fileData.Add(data);
  }
  <span class="hljs-keyword">return</span> View(fileData);
}
</code></pre><p>In this action method, I read the filed located in the ‘qrr’ folder using the code:</p>
<pre><code>Directory.GetFiles(<span class="hljs-string">"wwwroot/qrr"</span>)
</code></pre><p>Then I add each qrr file’s bytes and name inside a <code>List&lt;KeyValuePair&lt;string, Byte[]&gt;&gt;</code> object.</p>
<p>This object is returned to the View at the end:</p>
<pre><code><span class="hljs-keyword">return</span> View(fileData);
</code></pre><p>Finally create the ‘<code>ViewFile</code>’ View inside the ‘<code>Views/QRCoder</code>’ folder with the following code:</p>
<pre><code>@model List
&lt;KeyValuePair&lt;string, Byte[]&gt;&gt;
@{
    Layout = <span class="hljs-literal">null</span>;
}

  &lt;!DOCTYPE html&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">html</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width"</span> /&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Implementing QRCoder in ASP.NET Core - View QR Code Files<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
      <span class="hljs-selector-tag">body</span> {
        <span class="hljs-attribute">background</span>: <span class="hljs-number">#111</span> no-repeat;
        <span class="hljs-attribute">background-image</span>: <span class="hljs-built_in">-webkit-gradient</span>(radial, <span class="hljs-number">50%</span> <span class="hljs-number">0</span>, <span class="hljs-number">150</span>, <span class="hljs-number">50%</span> <span class="hljs-number">0</span>, <span class="hljs-number">300</span>, from(#<span class="hljs-number">444</span>), <span class="hljs-built_in">to</span>(#<span class="hljs-number">111</span>));
      }

      <span class="hljs-selector-tag">h1</span>,
      <span class="hljs-selector-tag">h2</span>,
      <span class="hljs-selector-tag">h3</span> {
        <span class="hljs-attribute">text-align</span>: center;
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
        <span class="hljs-attribute">margin</span>: <span class="hljs-number">5px</span> <span class="hljs-number">0</span>;
      }

      <span class="hljs-selector-tag">h1</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
      }

      <span class="hljs-selector-tag">h2</span> <span class="hljs-selector-tag">a</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#0184e3</span>;
        <span class="hljs-attribute">text-decoration</span>: none;
      }

      <span class="hljs-selector-tag">h3</span> {
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">23px</span>;
        <span class="hljs-attribute">border-bottom</span>: solid <span class="hljs-number">3px</span> <span class="hljs-number">#CCC</span>;
        <span class="hljs-attribute">padding-bottom</span>: <span class="hljs-number">10px</span>;
      }

      <span class="hljs-selector-tag">h3</span> <span class="hljs-selector-tag">a</span> {
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#00e8ff</span>;
        <span class="hljs-attribute">text-decoration</span>: none;
      }

      <span class="hljs-selector-tag">h3</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span>,
      <span class="hljs-selector-tag">h2</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
        <span class="hljs-attribute">text-decoration</span>: underline;
      }

      <span class="hljs-selector-class">.container</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
        <span class="hljs-attribute">margin</span>: auto;
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
        <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
      }

      <span class="hljs-selector-class">.container</span> <span class="hljs-selector-id">#content</span> {
        <span class="hljs-attribute">border</span>: dashed <span class="hljs-number">2px</span> <span class="hljs-number">#CCC</span>;
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
      }

      <span class="hljs-selector-id">#reset</span> {
        <span class="hljs-attribute">padding</span>: <span class="hljs-number">5px</span> <span class="hljs-number">10px</span>;
        <span class="hljs-attribute">background</span>: <span class="hljs-number">#4CAF50</span>;
        <span class="hljs-attribute">border</span>: none;
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF</span>;
        <span class="hljs-attribute">cursor</span>: pointer;
      }

      <span class="hljs-selector-id">#reset</span><span class="hljs-selector-pseudo">:hover</span> {
        <span class="hljs-attribute">color</span>: <span class="hljs-number">#4CAF50</span>;
        <span class="hljs-attribute">background</span>: <span class="hljs-number">#FFF</span>;
      }

      <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
      }

      <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> {
        <span class="hljs-attribute">height</span>: <span class="hljs-number">80px</span>;
        <span class="hljs-attribute">background</span>: darkcyan;
      }

      <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> <span class="hljs-selector-tag">td</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">50%</span>;
        <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">5px</span>;
      }

      <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> <span class="hljs-selector-tag">td</span> <span class="hljs-selector-tag">img</span> {
        <span class="hljs-attribute">width</span>: <span class="hljs-number">150px</span>;
      }

      <span class="hljs-selector-id">#viewContent</span> <span class="hljs-selector-tag">table</span> <span class="hljs-selector-tag">tr</span> <span class="hljs-selector-tag">td</span> <span class="hljs-selector-tag">span</span> {
        <span class="hljs-attribute">display</span>: block;
      }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"content"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Implementing QRCoder in ASP.NET Core - View QR Code Files<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">h2</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"http://www.yogihosting.com/category/aspnet-core/"</span>&gt;</span>Read the tutorial on YogiHosting » <span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"reset"</span> <span class="hljs-attr">onclick</span>=<span class="hljs-string">"location=''"</span>&gt;</span>Reset »<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"viewContent"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>
              @foreach (KeyValuePair
              <span class="hljs-tag">&lt;<span class="hljs-name">string,</span> <span class="hljs-attr">Byte</span>[]&gt;</span> k in Model) {
                <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"@String.Format("</span> <span class="hljs-attr">data:image</span>/<span class="hljs-attr">png</span>;<span class="hljs-attr">base64</span>,{<span class="hljs-attr">0</span>} ", <span class="hljs-attr">Convert.ToBase64String</span>(<span class="hljs-attr">k.Value</span>))" /&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>@k.Key<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
                  <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
                }
            <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>

  <span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span></span>
</code></pre><p>This View displays all the qrr files as bitmap images inside a ‘HTML’ table. The below code creates the HTML table:</p>
<pre><code>&lt;table&gt;
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>
    @foreach (KeyValuePair<span class="hljs-tag">&lt;<span class="hljs-name">string,</span> <span class="hljs-attr">Byte</span>[]&gt;</span> k in Model)
    {
      <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"@String.Format("</span><span class="hljs-attr">data:image</span>/<span class="hljs-attr">png</span>;<span class="hljs-attr">base64</span>,{<span class="hljs-attr">0</span>}", <span class="hljs-attr">Convert.ToBase64String</span>(<span class="hljs-attr">k.Value</span>))" /&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">span</span>&gt;</span>@k.Key<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
    }
  <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span></span>
</code></pre><h4 id="heading-testing-the-code-1"><strong>Testing the Code</strong></h4>
<p>Run your application and go to the URL — ‘<code>http://localhost:50755/QRCoder/ViewFile</code>’ to invoke the ViewFile Action method. You will see all the .qrr files saved in your website.</p>
<p>See the below image which illustrates it working:</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/S3jNmNaLIW0QuUy5qo9GV36lgPia8-qEgB2s" alt="Image" width="800" height="530" loading="lazy">
<em><strong>View all QRR files</strong></em></p>
<p><a target="_blank" href="https://github.com/yogyogi/QRCoder-implemented-in-ASP.NET-Core"><strong>You can download the full code from my GitHub Respositiory</strong></a><strong>.</strong></p>
<h3 id="heading-conclusion"><strong>Conclusion</strong></h3>
<p>I hope you love this repository which will help you to use QRCoder in your ASP.NET Core Project. Make sure you like this repository to show your love to it.</p>
<p>If you need any help in ASP.NET Core then let me know in the below comments section.</p>
<p><img src="https://cdn-media-1.freecodecamp.org/images/hHGcaGHoUc9cjgZiK5W7uBls4YgSY5wPewfR" alt="Image" width="300" height="374" loading="lazy"></p>
<p><em>I publish 2 web development articles per week. Consider following me and get email notification whenever I publish a new tutorial on Medium. If this post was helpful, please click the clap button for a few times to show your support! It will bring a smile on my face and motivate me to write more for the readers like you.</em></p>
<p>I have also published another tutorial on freeCodeCamp, if you would like to see it too — <a target="_blank" href="https://medium.freecodecamp.org/how-to-create-a-login-feature-with-bootstrap-modal-and-jquery-ajax-53dc0d281609">How to create a login feature with Bootstrap Modal and jQuery AJAX</a></p>
<p>Thanks and Happy Coding!</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
