Img,这是在wwwroot文件夹,不会显示与标记< img>,为什么?ASP.NET和ANGULAR

v6ylcynt  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(110)

所以,我的项目在名为'UsedCarsShop'的文件夹中,该文件夹包含两个文件夹:

  • 前端
  • 它包含'UsedCarsShopFrontend'文件夹,其中Angular项目位于
  • 后端
  • 它包含'CarWebShop'文件夹。在该文件夹中有另一个文件夹,相同的名称,和后端asp.net项目在最新的文件夹(CarWebShop)中有一个名为wwwroot/Photos的文件夹。每个用户的文件夹包含他们拥有的每辆汽车的图像。现在在我的HTML文档中,Angular应用程序中我有这个img标记<img src="/Photos/Bogi5/16/some_photo.jpg">

在program.cs文件中,我有这样的代码:

using CarWebShop.Data;
using CarWebShop.Interfaces;
using CarWebShop.Repository;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using System.Text;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddTransient<ICarRepository, CarsRepository>();
builder.Services.AddTransient<IAdverRepository, AdverRepository>();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddCors(options =>
{
    options.AddDefaultPolicy(builder =>
    {
        builder.WithOrigins("http://localhost:4200")
               .AllowAnyHeader()
               .AllowAnyMethod();
    });
});
builder.Services.AddDbContext<DataContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
builder.Services.AddControllers()
    .AddJsonOptions(options =>
    {
        options.JsonSerializerOptions.ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles;
    });
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options => {
                    options.TokenValidationParameters = new TokenValidationParameters
                    {
                        ValidateIssuer = true,
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                        ValidIssuer = builder.Configuration["Jwt:Issuer"],
                        ValidAudience = builder.Configuration["Jwt:Audience"],
                        IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
                    };
                });

var app = builder.Build();



// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.UseCors();

app.UseStaticFiles();


app.MapControllers();

app.Run();```

                      

As you can see, I am serving static files.
But Image do not show
Error is GET 'image path' (404) not found

字符串

xbp102n0

xbp102n01#

如果你将这两个服务作为独立的服务器运行,即ng serve和dotnet run,那么angular应用程序必须从后端服务器请求静态文件的图像。
第一个月

相关问题