Skip to content

Commit c472417

Browse files
committed
feat: add article about ASP.NET Core with Nuxt
1 parent 8492f13 commit c472417

File tree

4 files changed

+172
-1
lines changed

4 files changed

+172
-1
lines changed

content/1.posts/66.developer-experience-thoughts.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Thoughts about Developer Experience
3-
lead: Do not forget about DX when choosing a technology.
3+
lead: Do not forget about DX when choosing a technology
44
date: 2025-02-24
55
image:
66
src: /images/laptop_2.jpg
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
---
2+
title: "Integrating an ASP.NET Core API with a Nuxt Front End: A Step-by-Step Guide"
3+
lead: When ASP.NET Core Met Nuxt
4+
date: 2025-02-25
5+
image:
6+
src: /images/dotnet_nuxt_1.webp
7+
badge:
8+
label: Development
9+
tags:
10+
- ASP.NET Core
11+
- Nuxt
12+
- Vue.js
13+
- .NET
14+
---
15+
16+
In this article, I will walk you through the process of integrating an ASP.NET Core API with a Nuxt.js front end to have a stack that provides a nice developer experience to build web applications.
17+
18+
To make things easier to reproduce, we will create everything from scratch. I will do everything on the command line so that regardless of the IDE you are using (Rider, vscode, Visual Studio, etc) you will be able to understand what’s going on and follow along.
19+
20+
Let’s start by initializing a git repository in a new folder with a `gitignore` file for .NET:
21+
22+
```bash
23+
mkdir AspnetWithNuxt
24+
git init
25+
dotnet new gitignore
26+
```
27+
28+
We can create an empty .NET solution using the new `slnx` format to have a much cleaner and simple solution file:
29+
30+
```bash
31+
dotnet new sln -n AspnetWithNuxt--format slnx
32+
```
33+
34+
Then we can use the ASPNET Core Web API template to create the API:
35+
36+
```bash
37+
dotnet new webapi -o WebApi
38+
dotnet sln AspnetWithNuxt.slnx add WebApi\WebApi.csproj
39+
```
40+
41+
The generated ASP.NET Core API only contains one route `weatherforecast` that returns a list of random weather forecasts like these:
42+
43+
```json
44+
[
45+
{
46+
"date": "2025-02-24",
47+
"temperatureC": 40,
48+
"summary": "Cool",
49+
"temperatureF": 103
50+
},
51+
{
52+
"date": "2025-02-25",
53+
"temperatureC": 14,
54+
"summary": "Balmy",
55+
"temperatureF": 57
56+
}
57+
]
58+
```
59+
60+
We can now initialize the Nuxt project using the template “Nuxt 3 with v4 compat”. I will be using `pnpm` as my package manager, because [it’s performant and nice to use](https://pnpm.io/) (check this [blog article series](https://bordeauxcoders.com/series/pnpm-101) here if you want to learn more about pnpm).
61+
62+
```powershell
63+
pnpx nuxi init WebApp -t v4-compat --packageManager pnpm --gitInit false
64+
```
65+
66+
<div data-node-type="callout">
67+
<div data-node-type="callout-emoji">💡</div>
68+
<div data-node-type="callout-text">I could have used the default template or another <a target="_self" rel="noopener noreferrer nofollow" href="https://github.com/nuxt/starter?tab=readme-ov-file#templates" style="pointer-events: none">starter template</a> but this one will make my app compatible with Nuxt 4 when it is released.</div>
69+
</div>
70+
71+
The generated project just contains an `App.vue` that uses the built-in `NuxtWelcome` component. Let’s replace it with a new component `WeatherForecasts` that will make a call to the API and render the weather forecasts:
72+
73+
```vue
74+
<template>
75+
<WeatherForecasts/>
76+
</template>
77+
```
78+
79+
To initialize the `WeatherForecasts` component, we can use the `nuxi` CLI (using `nuxt` would also work by the way now that the project is initialized).
80+
81+
```bash
82+
pnpm nuxi add component WeatherForecasts
83+
```
84+
85+
The content of the component will be quite simple, the idea is to display data retrieved from the API.
86+
87+
```vue
88+
<script setup lang="ts">
89+
interface WeatherForecast {
90+
date: string;
91+
temperatureC: number;
92+
temperatureF: number;
93+
summary: string;
94+
}
95+
const { data: weatherForecasts } = await useFetch<WeatherForecast[]>('/api/weatherforecast');
96+
</script>
97+
<template>
98+
<div>
99+
<div v-if="weatherForecasts">
100+
<table>
101+
<thead>
102+
<tr>
103+
<th>Date</th>
104+
<th>Summary</th>
105+
<th>T (°C)</th>
106+
<th>T (°F)</th>
107+
</tr>
108+
</thead>
109+
<tbody>
110+
<tr v-for="item in weatherForecasts" :key="item.date">
111+
<td>{{ item.date }}</td>
112+
<td>{{ item.summary }}</td>
113+
<td>{{ item.temperatureC }}</td>
114+
<td>{{ item.temperatureF }}</td>
115+
</tr>
116+
</tbody>
117+
</table>
118+
</div>
119+
</div>
120+
</template>
121+
```
122+
123+
In the code, you can notice that the request is made to `/api/weatherforecast` and not to the URL of the `WebApi`. Indeed, we want to proxy the requests targeting `/api/**` to the back end for several reasons:
124+
125+
* Eliminate the need to configure specific CORS rules in the `WebApi`to prevent CORS errors due to different origins between the `WebApp` and the `WebApi`
126+
127+
* Avoid specifying the complete URL of the `WebApi` each time we make a request in a component
128+
129+
* Adopt a similar mechanism to the one that will be used when the application is deployed in the cloud (we will probably also proxy the traffic to the API but it will be directly configured in the infrastructure)
130+
131+
132+
As it is very well explained in this [video](https://youtu.be/J4E5uYz5AY8?si=cx0YkLdpiGWO1hYA) from Alexander Lichter, there are multiple strategies to do that, but using [Vite proxy](https://vite.dev/config/server-options#server-proxy) or [Nuxt/Nitro devProxy](https://nitro.build/config#devproxy) will only work on the client side and not when using Server Side Rendering. As Nuxt is a Vue framework that supports SSR by default, it would be a shame not to have the proxy work with SSR. So the solution is to configure the route rules in the Nuxt configuration file:
133+
134+
```typescript
135+
export default defineNuxtConfig({
136+
compatibilityDate: '2024-11-01',
137+
devtools: { enabled: true },
138+
future: {
139+
compatibilityVersion: 4
140+
},
141+
$development: {
142+
routeRules: {
143+
'/api/**': {
144+
proxy: 'http://localhost:5096/**',
145+
}
146+
},
147+
},
148+
})
149+
```
150+
151+
By placing this configuration in `$development`, I ensure it only applies to the development environment (locally, in my case). This is because I will likely handle request routing in my infrastructure configuration when my application is deployed in the cloud.
152+
153+
To finish, we can launch the front end and the back end:
154+
155+
```bash
156+
// Launch the Nuxt application front end
157+
pnpm --dir WebApp dev
158+
```
159+
160+
```bash
161+
// Launch the ASP.NET Core API
162+
dotnet run --project WebApi\WebApi.csproj
163+
```
164+
165+
Here are the weather forecasts retrieved from the API and displayed in the Front:
166+
167+
![A table showing dates with weather summaries and temperatures in Celsius and Fahrenheit.](/posts/images/67.temperatures.png){.rounded-lg.mx-auto}
168+
169+
Nothing fancy, but I hope this gives you a good understanding of how we can integrate an ASP.NET Core API with a Nuxt front end. Not everything is perfect, we have the URL of the API hardcoded in the Nuxt configuration, and we have to launch the API and the front end separately. In an upcoming article, we will explore how to use .NET Aspire to improve the developer experience.
170+
171+
You can find the complete source code used for this article in this [GitHub repository](https://github.com/TechWatching/AspnetWithNuxt).

public/images/dotnet_nuxt_1.webp

14.1 KB
Loading
7.33 KB
Loading

0 commit comments

Comments
 (0)