Home > Enterprise >  Images not being served in Asp .net core web api
Images not being served in Asp .net core web api

Time:04-15

i have a static folder calles Resources and inside it i have anothe folder called Images, and this is where i'm storing my images.

when i try to fetch an image, it's returning a 404 error not found

this is my startup.cs ConfigureServices code:

 public void ConfigureServices(IServiceCollection services)
        {
//.........

            services.Configure<FormOptions>(o => {
                o.ValueLengthLimit = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
                o.MemoryBufferThreshold = int.MaxValue;
            });
//....

And this is my configure code:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "storedProcedure v1"));
            }

            app.UseHttpsRedirection();

            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Resources\Images")),
                RequestPath = new PathString("/Resources/Images")
            });

            app.UseRouting();

            app.UseCors(
              options => options.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowCredentials()
            ); 

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}


any answer would be very appreciated, Thank you!

CodePudding user response:

I use your code, and it is work. You can refer to it.

My URL is: https://localhost:5001/Resources/Images/flower.PNG

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "staticImage", Version = "v1" });
            });

            services.Configure<FormOptions>(o => {
                o.ValueLengthLimit = int.MaxValue;
                o.MultipartBodyLengthLimit = int.MaxValue;
                o.MemoryBufferThreshold = int.MaxValue;
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "staticImage v1"));
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions()
            {
                FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Resources\Images")),
                RequestPath = new PathString("/Resources/Images")
            });
            app.UseRouting();
            app.UseCors(
             options => options.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowCredentials()
           );
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

Structure:

enter image description here

Result:

enter image description here

CodePudding user response:

It was a dumpy fault by me when I upload a Image using a post API my code was adding a number after that image name, Example: If the image name is flower.jpg it will become flower.jpg16 and than it will not be a valid image extension (.jpg16), I removed that number, And everything is working fine.

  • Related