简体   繁体   中英

How to run @Url.action written by string

I am trying to show image from database with @Url.Action method. The html document is also in database and I render it with @html.Raw function.

The problem is when I render the html including @Url.Action method it just show the whole function shape in the source parameter. The code I tried is below.

private string ConvertImageSource(int articleID, string content // the html string)
{
    var imageCount = // counting number of image;
    for (int i = 0; i < imageCount; i++)
    {
        content = content.Replace($"<!{i + 1}>", $"@Url.Action('ShowImage','Articles',new{{articleID={ articleID },imageID={ imageID }}})");
    }

    return content;
}

public ActionResult ShowImage(int? articleID, int? imageID)
{
    var imageData = Encoding.ASCII.GetBytes(// get image string from database);
    return new FileStreamResult(new MemoryStream(imageData), "image/jpeg");
}

I would like to know how to make it works. Any idea?

You may use that for loop on a partial view.

Looks like..

@model MyContentModel
@for (int i = 0; i < model.ImageCount; i++){{
@Url.Action("ShowImage", "Articles", new {{ articleID ={ model.ArticleID },imageID ={ model.ImageID } }})}}

Thanks for the answer. I found the solution by myself. Instead of @Url.Action function, I put url address and it works.

private string ConvertImageSource(int articleID, string content)
    {
        var imageCount = content.Split(new string[] { "img " }, StringSplitOptions.None).ToList().Count - 1;

        for(int i = 0; i < imageCount; i++)
        {
            content = content.Replace($"<!{i + 1}>", $"/Articles/ShowImage?articleID={ articleID }&imageID={i + 1}");
        }

        return content;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM