DOING: add another route...
npm create vite
> TypeScriptnpm create vite
> Customize with create-vuenpm create vite vue-csharp-fullstack-showcase-001
code vue-csharp-fullstack-showcase-001
npm i
npm run dev
cd vue-csharp-fullstack-showcase-001
dotnet --version
dotnet new webapi -n backend
code backend
dotnet run --urls "http://localhost:5200"
npm i -D concurrently
npm i -D nodemon
#!/bin/bash# Set the desired URLURL="http://localhost:5200"# Run the .NET application with the specified URLdotnet run --urls "$URL"
"scripts": {"dev": "conc \"npm run api\" \"npm run frontend\"","api": "nodemon","frontend": "vite --port 5100 --open","build": "run-p type-check \"build-only {@}\" --","preview": "vite preview","build-only": "vite build","type-check": "vue-tsc --build","lint": "eslint . --fix","format": "prettier --write src/"},"nodemonConfig": {"watch": ["backend"],"ext": "cs","exec": "cd backend && bash ./run-api.sh"},
var builder = WebApplication.CreateBuilder(args);builder.Services.AddEndpointsApiExplorer();builder.Services.AddSwaggerGen();builder.Services.AddCors(options =>{options.AddPolicy("AllowAll",policy => policy.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());});var app = builder.Build();if (app.Environment.IsDevelopment()){app.UseSwagger();app.UseSwaggerUI();}app.UseCors("AllowAll");app.UseHttpsRedirection();var summaries = new[]{"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"};app.MapGet("/weatherforecast", () =>{var forecast = Enumerable.Range(1, 5).Select(index =>new WeatherForecast(DateOnly.FromDateTime(DateTime.Now.AddDays(index)),Random.Shared.Next(-20, 55),summaries[Random.Shared.Next(summaries.Length)])).ToArray();return forecast;}).WithName("GetWeatherForecast").WithOpenApi();app.Run();record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary){public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);}
<script setup lang="ts">import { ref, onMounted } from 'vue'interface WeatherData {date: stringtemperatureC: numbertemperatureF: numbersummary: string}const weatherData = ref<WeatherData[]>([])const fetchWeather = async () => {try {const response = await fetch('http://localhost:5200/weatherforecast')if (!response.ok) throw new Error('Failed to fetch weather data')weatherData.value = await response.json()} catch (error) {console.error('Error fetching weather data:', error)}}onMounted(fetchWeather)</script><template><div><h2>Weather Forecast</h2><ul v-if="weatherData.length"><li v-for="weather in weatherData" :key="weather.date"><strong>{{ weather.date }}</strong> -{{ weather.temperatureC }}°C / {{ weather.temperatureF }}°F -<em>{{ weather.summary }}</em></li></ul><p v-else>Loading weather data...</p></div></template><style scoped>h2 {margin-bottom: 10px;}ul {list-style: none;padding: 0;}li {padding: 5px 0;}</style>