簡體   English   中英

如何運行由字符串編寫的@ Url.action

[英]How to run @Url.action written by string

我正在嘗試使用@Url.Action方法顯示數據庫中的圖像。 html文檔也位於數據庫中,我使用@html.Raw函數將其呈現。

問題是當我渲染包含@Url.Action方法的html時,它只是在source參數中顯示整個函數形狀。 我嘗試過的代碼如下。

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");
}

我想知道如何使其運作。 任何想法?

您可以在局部視圖上使用該for循環。

看起來像..

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

感謝您的回答。 我自己找到了解決方案。 我沒有使用@ Url.Action函數,而是使用了url地址,它可以正常工作。

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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM