Reagieren Sie die Anwendung in C# [geschlossen]C#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Reagieren Sie die Anwendung in C# [geschlossen]

Post by Anonymous »

Ich schreibe eine Anwendung in React- und C# -Programmiersprachen und es möchte nicht funktionieren. Bitte helfen Sie.import React from 'react';

function Header() {
return(

Sklep z owocami i warzywami

);
}

export default Header;
< /code>
productList.js
import React, { useState, useEffect } from 'react';
import Product from './Product';

function ProductList() {
const [products, setProducts] = useState([]);

useEffect(() => {
fetch('https://localhost:5001/api/products')
.then(response => response.json())
.then(data => setProducts(data));
}, []);

return (

{products.map(product => (

))}

);
}

export default ProductList;
< /code>
Product.js
import React from 'react';

function Product ({product}) {
return(

{product.name}
{product.price} PLN

)
}

export default Product;
< /code>
app.js
import React from 'react';
import Header from './components/Header';
import ProductList from './components/ProductList';
import './App.css';

function App() {
return (



);
}

export default App;
< /code>
progrom.cs c#< /p>
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

namespace FruitVegetableShopAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
[HttpGet]
public IEnumerable Get()
{
return new List
{
new Product { Id = 1, Name = "Jabłko", Price = 2.99M },
new Product { Id = 2, Name = "Marchewka", Price = 1.99M },
new Product { Id = 3, Name = "Ziemniak", Price = 2.69M },
new Product { Id = 4, Name = "Pietruszka", Price = 5.99M },
new Product { Id = 5, Name = "Seler", Price = 4.99M },
new Product { Id = 6, Name = "Gruszka", Price = 4.49M },
new Product { Id = 7, Name = "Mango", Price = 5.99M },
new Product { Id = 8, Name = "Por", Price = 5.49M },
new Product { Id = 9, Name = "Kalafior", Price = 7.99M },
new Product { Id = 10, Name = "Koperek", Price = 4.50M },
new Product { Id = 11, Name = "Śliwka", Price = 7.50M },
new Product { Id = 12, Name = "Cytryna", Price = 5.99M },
new Product { Id = 13, Name = "Fasola", Price = 6.99M },
new Product { Id = 14, Name = "Banan", Price = 5.40M },
new Product { Id = 15, Name = "Pomarańcza", Price = 6.99M }
};
}
}

public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
}
< /code>
startup.cs c#< /p>
void ConfigureServices(IServiceCollection services)
{
services.AddCors(options =>
{
options.AddDefaultPolicy(
builder =>
{
builder.WithOrigins("http://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddControllers();
}

void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseCors();

app.UseRouting();

app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
< /code>
Nach dem Schreiben dachte ich, es würde funktionieren, aber es tut es nicht. Ich weiß nicht, ob ich etwas in den Code aufgenommen habe. Ich schreibe zum ersten Mal in der React -Programmiersprache

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post