---
title: "Use project Tye to host Blazor WASM and ASP.NET Web API on a single origin to avoid CORS"
excerpt: "Using Microsoft's experimental Project Tye, you configured the proxy to forward requests to '/api' to the Web API, and all other requests to the Blazor WASM client."
publishDate: "2021-02-14T00:00:00"
createDate: "2021-02-14T03:27:52.547"
updateDate: "2021-02-15T12:38:33.763"
category: "dotnet"
categoryName: ".NET"
tags: ["dotnet",".NET","dotnetcore",".NET Core","ASP.NET","ASP.NET Core","aspnetcore","C#","Blazor","WebAssembly","WebAPI","Tye"]
permalink: "/blog/dotnet/use-project-tye-to-host-blazor-wasm-and-aspdotnet-web-api-on-a-single-origin"
canonical: "/blog/dotnet/use-project-tye-to-host-blazor-wasm-and-aspdotnet-web-api-on-a-single-origin"
image: {"src":"/media/mxmnuxaf/dotnet-project-tye.png","focalPoint":{"left":0.5,"top":0.49933732803195763},"crops":[{"alias":"Banner","width":1120,"height":400,"coordinates":null}]}
imageAlt: ".NET Bot next to title: Use project Tye to host Blazor WASM and ASP.NET Web API on a single origin"
imageBackgroundColor: "#fff"
thumbnailImage: {"src":"/media/ggrmwooo/dotnet-yarp-square.png","focalPoint":{"left":0.5,"top":0.5002459542608448},"crops":[{"alias":"thumbnail","width":250,"height":250,"coordinates":null}]}
thumbnailImageAlt: ".NET Bot building an application"
metaTitle: "Use project Tye to host Blazor WASM and ASP.NET Web API on a single origin to avoid CORS"
metaDescription: "Using Microsoft's experimental Project Tye, you configured the proxy to forward requests to '/api' to the Web API, and all other requests to the Blazor WASM client."
ogTitle: "Use project Tye to host Blazor WASM and ASP.NET Web API on a single origin to avoid CORS"
ogDescription: "Using Microsoft's experimental Project Tye, you configured the proxy to forward requests to '/api' to the Web API, and all other requests to the Blazor WASM client."
ogImage: {"src":"/media/ofzhqoz0/dotnet-project-tye-social.png","focalPoint":{"left":0.5,"top":0.5014965545917087},"crops":[{"alias":"OpenGraph","width":1200,"height":630,"coordinates":null}]}
ogImageAlt: ".NET Bot next to title: Use project Tye to host Blazor WASM and ASP.NET Web API on a single origin"
relatedPostKeys: ["3fa142faee1a488c91f507627e8059bb","0da0d49da8fd41aaad1f4a7accdffe68"]
noIndex: false
noFollow: false
sitemapExclude: false
source: {"nodeId":1431,"contentKey":"39dcf08751ac4bbeaf9da1753dd718c1","sortOrder":83}
---


Applications are often split up into a client and a server. The client renders the UI and communicates to the server through an API. But when the client and server are split up into separate projects and hosted at different origins (**origin == scheme + host + port**), it can be a hassle for the client to communicate with the server.


On the web specifically, communicating between different origins can be painful. By default, clients are not allowed to make ajax requests to a different origin. The server has to use **Cross-Origin Resource Sharing (CORS)** headers to allow cross-origin ajax requests.


Another way to resolve CORS challenges is to circumvent it entirely by having the client and server hosted on the same origin. If your technology stack allows for it, you can merge the client and server into a single project and host them on a single origin. If that's not possible, you can **use a reverse proxy to consolidate the client origin and the server origin under the proxy's origin**.




<p><img style="display: block; margin-left: auto; margin-right: auto;" src="/media/no5fagrc/reverseproxy.svg" alt="A diagram: web browser points to reverse proxy server. Reverse proxy server points to two web servers. The first web server is an API server. The second server is a frontend website server. The diagram visualizes how the HTTP requests from a web browser are sent to the reverse proxy. If the requested path starts with &quot;/API/&quot;, the proxy forwards the request to the API server. Otherwise the request is sent to the Client Web Server which serves the frontend of your application." width="500" height="225.5017709563164"></p>




The diagram above visualizes how the HTTP requests from a web browser are sent to the reverse proxy. If the requested path starts with '*/API/*', the proxy forwards the request to the API server. Otherwise, the request is sent to the Client Web Server which serves the frontend of your application.





You can pick any option for your frontend client, backend server, and reverse proxy server. It does not matter because they all understand the same language: **HTTP**. In this walkthrough, you'll learn how to communicate between a **Blazor WebAssembly (WASM)** client and an **ASP.NET Web API** <span style="text-decoration: underline;"><strong>without CORS</strong></span>.





In a previous tutorial, you learned <a href="/blog/dotnet/use-yarp-to-host-client-and-api-server-on-a-single-origin" title="Use YARP to host client and API server on a single origin">how to achieve this using Microsoft's new reverse proxy "YARP"</a>. You then used Project Tye, Microsoft's <span style="text-decoration: underline;">experimental</span> developer tool to spin up the client, server, and proxy with a single command. You can also use **Project Tye**'s built-in proxy server instead of YARP.


In this walkthrough, the client and server will be hosted on a single origin by using Tye's ingress feature.

<p><img style="display: block; margin-left: auto; margin-right: auto;" src="/media/brbashxb/reverseproxytye.svg" alt="A diagram: web browser points to the Tye Ingress. Tye Ingress points to two web servers. The first web server is an ASP.NET Core WebAPI server. The second server is a Blazor WebAssembly server. The diagram visualizes how the HTTP requests from a web browser are sent to the Tye Ingress. If the requested path starts with &quot;/API/&quot;, the proxy forwards the request to the ASP.NET Core WebAPI server. Otherwise the request is sent to the Client Web Server which serves the Blazor WebAssembly application." width="500" height="228.4688995215311" loading="lazy" decoding="async"></p>




This is how the [Tye docs define an Ingress](https://github.com/dotnet/tye/blob/master/docs/recipes/ingress.md):




<blockquote>For modern cloud runtime environments like Kubernetes, it's typical to keep the vast majority of services isolated from public internet traffic. Usually, there's a single (or few) point of entry for public traffic using a highly configurable proxy server. <br><br>This approach is referred to as ingress. The proxy server and surrounding management infrastructure are referred to as an ingress controller and the configurations of routing rules are referred to as ingresses.</blockquote>

This sounds exactly like a reverse proxy, but Tye will refer to it as an Ingress throughout their documentation and configuration.

## Prerequisites {#prerequisites}

To follow along you need the following tech:

- .NET Core 3.1 or up (<a rel="noopener" href="https://dotnet.microsoft.com/download" target="_blank">installation instructions</a>)
- Windows PowerShell or PowerShell Core (<a rel="noopener" href="https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell" target="_blank">installation instructions</a>)
- OS which supports the above tech such as Windows, Mac, or Linux



<p class="panel panel--info">You can find all the source code for the end result on <a rel="noopener" href="https://github.com/Swimburger/TyeClientServerSingleOrigin" target="_blank">GitHub</a>.</p>



## Create the Blazor WASM client {#create-the-blazor-wasm-client}

Before creating the client, create a folder where all projects will reside using the following commands:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">mkdir</span> <span style="color:#0000e6; ">TyeClientServerSingleOrigin</span>&#10;<span style="color:#005fd2; ">cd</span> <span style="color:#0000e6; ">TyeClientServerSingleOrigin</span>&#10;</pre>

Create a solution file using the dotnet command line interface (CLI):

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">dotnet</span> <span style="color:#0000e6; ">new</span> <span style="color:#0000e6; ">sln</span>&#10;</pre>

Create the Blazor WASM client:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">dotnet</span> <span style="color:#0000e6; ">new</span> <span style="color:#0000e6; ">blazorwasm</span> <span style="color:#074726; ">-n</span> <span style="color:#0000e6; ">Client</span> <span style="color:#074726; ">-o</span> <span style="color:#0000e6; ">Client</span>&#10;</pre>




**\-n**: specifies the name of the project





**\-o**: specifies the location where the project will be created


Using the command above, you created a Blazor WASM project named 'Client' which is also used as the default namespace. This project is created in a subfolder also named 'Client'.

Add the client project to the solution file:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">dotnet</span> <span style="color:#0000e6; ">sln</span> <span style="color:#0000e6; ">add</span> <span style="color:#0000e6; ">Client</span>&#10;</pre>




When you take a look at the FetchData component at *Client\\Pages\\FetchData.razor*, you can see the client is requesting data about the weather from a static JSON file:




<pre style="color:#000000;background:#ffffff;">forecasts <span style="color:#808030; ">=</span> await Http<span style="color:#808030; ">.</span>GetFromJsonAsync<span style="color:#808030; ">&lt;</span>WeatherForecast<span style="color:#808030; ">[</span><span style="color:#808030; ">]</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">(</span><span style="color:#800000; ">"</span><span style="color:#0000e6; ">sample-data/weather.json</span><span style="color:#800000; ">"</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>&#10;</pre>

Update this line of code to fetch the data from `api/WeatherForecast` instead:

<pre style="color:#000000;background:#ffffff;">forecasts <span style="color:#808030; ">=</span> await Http<span style="color:#808030; ">.</span>GetFromJsonAsync<span style="color:#808030; ">&lt;</span>WeatherForecast<span style="color:#808030; ">[</span><span style="color:#808030; ">]</span><span style="color:#808030; ">&gt;</span><span style="color:#808030; ">(</span><span style="color:#800000; ">"</span><span style="color:#0000e6; ">api/WeatherForecast</span><span style="color:#800000; ">"</span><span style="color:#808030; ">)</span><span style="color:#800080; ">;</span>&#10;</pre>

This API endpoint does not exist yet, but you will create this soon.

## Create the ASP.NET Core Web API server {#create-the-aspnet-core-web-api-server}

Create the Web API project using the dotnet CLI:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">dotnet</span> <span style="color:#0000e6; ">new</span> <span style="color:#0000e6; ">webapi</span> <span style="color:#074726; ">-n</span> <span style="color:#0000e6; ">Server</span> <span style="color:#074726; ">-o</span> <span style="color:#0000e6; ">Server</span>&#10;</pre>

Using the command above, you created a Web API project named 'Server' which is also used as the default namespace. This project is created in a subfolder also named 'Server'.

Add the server project to the solution file:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">dotnet</span> <span style="color:#0000e6; ">sln</span> <span style="color:#0000e6; ">add</span> <span style="color:#0000e6; ">Server</span>&#10;</pre>



<p>When you try to run the client and server at the same time, an error will be shown because they use the same port by default. The ports used by these projects are specified at '<em>Properties\launchSettings.json</em>'. To prevent port collision, replace '<strong>https://localhost:5001;http://localhost:5000</strong>' with '<strong>https://localhost:5003;http://localhost:5002</strong>' at '<em>Server\Properties\launchSettings.json</em>'. Or run these PowerShell commands:</p>



<pre style="color:#000000;background:#ffffff;"><span style="color:#797997; ">$launchSettingsContents</span> <span style="color:#44aadd; ">=</span> <span style="color:#666616; ">Get-Content</span> <span style="color:#074726; ">-path</span> <span style="color:#0000e6; ">./Server/Properties/launchSettings.json</span> <span style="color:#074726; ">-Raw</span>&#10;<span style="color:#797997; ">$launchSettingsContents</span> <span style="color:#44aadd; ">=</span> <span style="color:#797997; ">$launchSettingsContents</span> <span style="color:#800000; font-weight:bold; ">-replace</span> <span style="color:#800000; ">'</span><span style="color:#0000e6; ">https://localhost:5001;http://localhost:5000</span><span style="color:#800000; ">'</span><span style="color:#44aadd; ">,</span><span style="color:#800000; ">'</span><span style="color:#0000e6; ">https://localhost:5003;http://localhost:5002</span><span style="color:#800000; ">'</span>&#10;<span style="color:#666616; ">Set-Content</span> <span style="color:#074726; ">-Value</span> <span style="color:#797997; ">$launchSettingsContents</span> <span style="color:#074726; ">-Path</span> <span style="color:#0000e6; ">./Server/Properties/launchSettings.json</span>&#10;</pre>




There is a single controller that comes out of the box with the WebAPI template: WeatherForecastController.cs<br>This controller generates random weather forecasts and luckily for you matches the data model used in the Blazor WASM client. The WeatherForecast API can be requested at '*/WeatherForecast*', but the client will request it at '*/api/WeatherForecast*'. The reverse proxy you will create next will suffix the API with '*/api*'.




## Tying everything up with Tye {#tying-everything-up-with-tye}

Install Tye as a .NET global tool:

<pre style="color:#000000;background:#ffffff;">dotnet <span style="color:#2a00ff; ">tool</span> <span style="color:#2a00ff; ">install</span> -g <span style="color:#2a00ff; ">Microsoft.Tye</span> <span style="color:#2a00ff; ">--version</span> <span style="color:#2a00ff; ">"</span><span style="color:#2a00ff; ">0.6.0-alpha.21070.5</span><span style="color:#2a00ff; ">"</span>&#10;</pre>




You can find the most recent version of <a rel="noopener" href="https://www.nuget.org/packages/Microsoft.Tye" target="_blank">Tye at Nuget</a>.


Initialize tye using this command:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">tye</span> <span style="color:#0000e6; ">init</span>&#10;</pre>




This will create a configuration YAML file '*tye.yaml*'. Update the YAML file to match the content below:




<pre style="color:#000000;background:#ffffff;">name: tyeclientserversingleorigin&#10;services:&#10;- name: client&#10;  project: Client/Client.csproj&#10;  bindings:&#10;  - port: 5001&#10;    protocol: https&#10;- name: server&#10;  project: Server/Server.csproj&#10;  bindings:&#10;  - port: 5003&#10;    protocol: https&#10;ingress:&#10;  - name: ingress&#10;    bindings:&#10;      - port: 8080&#10;        protocol: https&#10;    rules:&#10;      - path: /api&#10;        service: server&#10;      - path: /swagger&#10;        preservePath: true&#10;        service: server&#10;      - service: client&#10;</pre>

Supposedly, you should be able to omit the binding in the tye configuration, but some things were very buggy when doing so. So for now you should specify the port and protocol for the client, server, and ingress.<br>This YAML file will configure tye to

- run the client at <em>https&#58;//localhost:5001</em>
- run the server at <em>https&#58;//localhost:5003</em>
- run an ingress (reverse proxy) at <strong><em>https&#58;//localhost:8080 </em></strong>with the following rules
  - any request starting with path <em>/api</em> will be routed to the server service. <br>By default, the defined path will be trimmed. HTTP requests to <em>https&#58;//localhost:8080/api/weatherforecast</em> will be proxied to <em>https&#58;//localhost:5003/weatherforecast</em>. Note the <em>/api</em> prefix has been removed.
  - any request starting with path <em>/swagger</em> will be routed to the server service. The '<strong>preservePath</strong>' property will ensure that '/swagger' will not be trimmed. HTTP requests to <em>https&#58;//localhost:8080/swagger </em>will be proxied to <em>https&#58;//localhost:5003/swagger</em>.<br>Swagger's JavaScript will request data relative to the root, not relative to <em>/api</em>, so you need to add this rule to fix that.
  - any request which didn't match any of the previous rules will be proxied to the client service. For example, <em>https&#58;//localhost:8080/counter </em>will be routed to <em>https&#58;//localhost:5001/counter</em>.

Run this command to run all the services and ingress:

<pre style="color:#000000;background:#ffffff;"><span style="color:#005fd2; ">tye</span> <span style="color:#0000e6; ">run</span>&#10;</pre>



<p>Open a browser and navigate to <em><strong>https://localhost:8080</strong></em>. The Blazor UI should appear. <br>When you navigate to the Fetch Data section, the weather forecasts should be updating with random results every time you refresh because the data is coming from the server ASP.NET WebAPI project.</p>




You can also navigate to tye's dashboard at <strong><em>localhost:8000</em></strong><em>:</em>




<picture>
  <img src="https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=1120/media/sydd1iqw/tye-dashboard.png" srcset="https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=430/media/sydd1iqw/tye-dashboard.png 430w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=645/media/sydd1iqw/tye-dashboard.png 645w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=718/media/sydd1iqw/tye-dashboard.png 718w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=860/media/sydd1iqw/tye-dashboard.png 860w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=1077/media/sydd1iqw/tye-dashboard.png 1077w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=1120/media/sydd1iqw/tye-dashboard.png 1120w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=1436/media/sydd1iqw/tye-dashboard.png 1436w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=1680/media/sydd1iqw/tye-dashboard.png 1680w, https://swimburger.net/cdn-cgi/image/format=auto,quality=90,width=2240/media/sydd1iqw/tye-dashboard.png 2240w" sizes="(max-width: 480px) 430px, (max-width: 768px) 718px, 1120px" width="1120" height="345" alt="Screenshot of the project Tye dashboard" loading="lazy" decoding="async">
</picture>

In addition to finding the URL for each service, you can also watch the log streams of each service.

## Summary {#summary}




API's need to provide CORS headers to explicitly allow ajax requests from web clients hosted on a different origin. Reverse proxies can merge the API origin and client origin onto a single origin. Using Microsoft's experimental Project Tye, you can configure an ingress/proxy to forward requests from '*/api*' to the Web API, and all other requests to the Blazor WASM client.





You can learn more about project Tye on <a rel="noopener" href="https://github.com/dotnet/tye" target="_blank">its GitHub repository</a>.
