What is the difference between <% %> and <%=%>?

前端 未结 4 2328
庸人自扰
庸人自扰 2020-11-29 01:41

What is the difference between <% %> and <%= %> in ASP.NET MVC? And when to use which?

4条回答
  •  臣服心动
    2020-11-29 02:09

    Say you have a method on your page, called "SayHello":

    protected string SayHello()
    {
        return "Hello!";
    }
    

    And on your page, you have these statements:

    first: <%= SayHello() %>
    second: <% SayHello() %>
    

    Your output will be:

    first: Hello!
    second: 
    

    when you use <%= %>, what you put in there is inserted into the html at that position. If you use <% %>, you're just inserting some code into your page.

提交回复
热议问题