Display HTML String in ASP.Net Core Razor Pages

sunkana
 
on Jan 09, 2022 10:04 PM
3169 Views

Hi,

I am trying to return to UI a string embedded with link in simple .NET Core razor page code behind C#.

The code is as

string myUrl = "https://bnsf.service-now.com/sp?id=sc_cat_item&sys_id=9feb4125db025050ac30389f9d9619ee";
status.Message = "Click" + "<a href= '" + myUrl +"'>Here</a>";
msg = status.Message;
return msg;

The above code is not working, myURL is displayed as plain text.

Please let me know which namespaces to be included in the to display myURL as link on UI.

Thanks.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jan 09, 2022 11:00 PM

Hi sunkana,

Use HtmlHelper.Raw that rendered as HTML markup.

Check this example. Now please take its reference and correct your code.

Index Model

public class IndexModel : PageModel
{
    public string Message { get; set; }
    public void OnGet()
    {
        this.Message = GetMessage();
    }

    public string GetMessage()
    {
        string myUrl = "https://www.aspsnippets.com";
        return "Click " + "<a href= '" + myUrl + "' target='_blank'>Here</a>";
    }
}

Razor View

@page
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@model Link_Message_Razor.Pages.IndexModel
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @Html.Raw(Model.Message)
</body>
</html>

Screenshot