locked
use static files from routes controllers in .net core RRS feed

  • Question

  • User338455301 posted

    i define this route to my action but my static (.png / js / css , ...) file go to this action 
    how to change it ? 

       [HttpGet("{culture}/products/{id?}/{search?}")]
    
            public async Task<IActionResult> Index(string id, string search)
            {
            }

    this file use this route : 

    https://www.chelbicarpet.com/Files/Products/Thumb/ed5f0a69-9154-4939-9918-993548faa05f.JPG




    Thursday, August 13, 2020 4:58 AM

All replies

  • User711641945 posted

    Hi elahi1mahdi,

    Be sure the image(ed5f0a69-9154-4939-9918-993548faa05f.JPG) located in the `wwwroot/Files/Products/Thumb` folder.

    And be sure the UseStaticFiles middleware call before routing:

    app.UseStaticFiles();
    
    app.UseRouting();
    
    app.UseAuthorization();
    
    app.UseEndpoints(endpoints =>
    {               
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });   

    If the image does not locate in the default wwwroot folders,it locates in the `Files/Products/Thumb` folder.

    Configure your middleware like below:

    //app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(env.ContentRootPath, "Files")),
            RequestPath = "/Files"
    });
    app.UseRouting();
    app.UseAuthorization();
    app.UseEndpoints(endpoints =>
    {               
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });       

    Reference:

    https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-3.1

    Best Regards,

    Rena

    Thursday, August 13, 2020 5:50 AM