What, why or when it is better to choose cshtml vs aspx?

前端 未结 4 628
暖寄归人
暖寄归人 2021-01-29 23:32

I would like to know what, why or when it is better to choose cshtml and what, why or when it is better to choose aspx technologies? What are these two technologies intended for

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 23:56

    Razor is a view engine for ASP.NET MVC, and also a template engine. Razor code and ASP.NET inline code (code mixed with markup) both get compiled first and get turned into a temporary assembly before being executed. Thus, just like C# and VB.NET both compile to IL which makes them interchangable, Razor and Inline code are both interchangable.

    Therefore, it's more a matter of style and interest. I'm more comfortable with razor, rather than ASP.NET inline code, that is, I prefer Razor (cshtml) pages to .aspx pages.

    Imagine that you want to get a Human class, and render it. In cshtml files you write:

    Name is @Model.Name

    While in aspx files you write:

    Name is <%= Human.Name %>

    As you can see, @ sign of razor makes mixing code and markup much easier.

提交回复
热议问题