dmr
dmr

Reputation: 22403

HTML tags showing in page title

I am setting page titles dynamically, based on the on a property in the page's model. Sometimes that property contains html tags, such as <i>Article Title</i>. When the property contains HTML tags, they are showing in the page title. I tried surrounding the property with @Html.Raw, but it didn't help. How can I make sure the tags don't show?

Code:

<head>
    <title>@Html.Raw(Model.Title)</title>
</head>

Working code based on @Santiago's answer:

<head>
@{
    string titleRaw = Model.Title;
    string htmlTag = "<[^>]*>";
    Regex rgx = new Regex(htmlTag);
    string titleToShow = rgx.Replace(titleRaw, blank);
}
    <title>@titleToShow</title>
</head>

Upvotes: 1

Views: 747

Answers (1)

Santiago Rebella
Santiago Rebella

Reputation: 2449

Could you try to do it with regular expressions?

<[^>]*>

Took that from:

Regular expression to remove HTML tags from a string

String target = someString.replaceAll("<[^>]*>", "");

Assuming your non-html does not contain any < or > and that your input string is correctly structured.

A more complete explanation is available in above SO link

Upvotes: 2

Related Questions