Write HTML to string

前端 未结 18 1565
一生所求
一生所求 2020-12-13 00:04

I have code like this. Is there a way to make it easier to write and maintain? Using C# .NET 3.5.

string header(string title)
{
    StringWriter s = new Stri         


        
18条回答
  •  难免孤独
    2020-12-13 00:32

    I know you asked about C#, but if you're willing to use any .Net language then I highly recommend Visual Basic for this exact problem. Visual Basic has a feature called XML Literals that will allow you to write code like this.

    Module Module1
    
        Sub Main()
    
            Dim myTitle = "Hello HTML"
            Dim myHTML = 
                             
                                 <%= myTitle %>
                             
                             
                                 

    Welcome

    IDName
    1CouldBeAVariable
    Console.WriteLine(myHTML) End Sub End Module

    This allows you to write straight HTML with expression holes in the old ASP style and makes your code super readable. Unfortunately this feature is not in C#, but you could write a single module in VB and add it as a reference to your C# project.

    Writing in Visual Studio also allows proper indentation for most XML Literals and expression wholes. Indentation for the expression holes is better in VS2010.

提交回复
热议问题