简体   繁体   中英

Validate Razor template

How can I validate Razor template, which is used to render mail template.

string template = @"
My template 
@if (Model.Condition { <span>is true</spaM> }"

I made two mistakes: missing closing bracket and wrong closing tag. I need to validate it and known what should I fix (the best is to known in which line). I would like to use native Razor methods.

If I understand correctly, you want to be notified that the code you've written in the Template is invalid HTML.

If so, I'm afraid there is no easy way. The Template is purely producing text that you specify to go out to the response.

It may not even be HTML - could be JavaScript or a number of other outputs - it's just a string of text.

You may have a Template that produces the start of a table, another that produces the body, and another that produces the footer and end table tags. Each of these would produce invalid HTML on their own, but output one after the other would produce a valid HTML table. (That's a lot of produces there - sorry).

What would make it invalid is the parser of the HTML - ie the browser. You would only be able to validate your Template output when it is in a complete document that can then be parsed.

You mean ?

@{
    string template = Model.Condition ? "My template <span>is true</span>" : "";
}
string MyString = string.empty;
@if(Model.Condition)
{
MyString ="<span>"+ "is true"+"</span>";
}

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