Search code examples
c#asp.net-corerazor-pages

Configure Routing on RazorPages


Configure routing to enable getting images by not only the standard path by images/{image_id}

For images pages how can I configure routing like that?


    @page
    @model GetandTake.Pages.Categories.ImagesModel
    <h3>All Images</h3>
    @foreach (var category in Model.Categories)
    {
        @if (@category.ImagePath != null)
        {
            string imagePath = $"https://localhost:7005/" + @category.ImagePath;
            <img src="@imagePath" class="img-thumbnail" width="250px;" />
        }
    }


Solution

  • Here is a working demo about showing images in wwwroot:

    Configuration:
    ...
      app.UseHttpsRedirection();
    
        app.UseStaticFiles();
    
        app.UseRouting();
    ...
    

    wwwroot:

    enter image description here

    view:

    @foreach (var category in Model.Categories)
        {
            @if (@category.ImagePath != null)
            {
                <img src="/@category.ImagePath" class="img-thumbnail" width="250px;" />
               //category.ImagePath is 1.PNG here
            }
        }