05.04.2018, 17:11 | #1 |
Участник
|
sertandev: Simple server-side scripting with ASP.NET Razor
Источник: http://devblog.sertanyaman.com/2018/...asp-net-razor/
============== With the introduction of .NET framework and ASP.NET in the late 1990s, the legacy HTML embedded single page web application model of ASP is replaced by the Web Forms (and later ASP.NET MVC model). While the new ASP.NET models brought many new possibilities and made it easier to create enterprise web applications, the simplicity of legacy ASP was left behind. If you are a hobby web developer and create web applications only to provide content in your own websites, writing ASP.NET web forms and code behind files, or model view and controller code for your single web page could be an overkill. In June 2010, To provide a simpler server-side programming tool for web developers, Microsoft released the Razor scripting and Web Matrix tool set as a part of MVC 3, and called the new single page programming model “ASP.NET Web Pages” (Yes, “Web pages”). Today it is the easiest server-side programming model available for you compared to classic ASP and PHP options. Below I give you a quick summary of ASP.NET Web Pages programming model and Razor syntax. Razor syntax To embed code in your HTML page using Razor, you simply add your server-side code inside a @{ } block: @{var myVariable = "Hello World";}If you want to output a .NET variable inside HTML, it is as simple as using it together with @ operator inside your html code : Hello World My message is @myVariable It is also possible to output your variables to your document inside a code block like below: @{if (IsPost){ string url = Request.QueryString["redirurl"]; string decoded = Server.UrlDecode(url); //You can output variables to your document Write(decoded); //or inside razor specific tag @decoded //Or using any other html markup inside your code @decoded //Or like this using @: @: @decoded }else{ RenderPage("~/OtherPage");}} You can also embed server side loops and if statements with @ operator:
Layouts Using Razor, it is possible create a layout page (similar to master pages in ASP.NET) and use it in your other content pages like below : This is my homepage layout @RenderBody()My Footer @{Layout="Layout.cshtml";}This is my content in other file, displayed inside my chosen layout.Globals To initialize your global variables, you place your initialization code inside a special page named “_AppStart.cshtml” which runs when the web application starts. The “Page” object in Razor is a special object that you can add new properties inside and access them globally from your code : _AppStart.cshtml@{Page.MyChoice = "Razor";Page.WebSiteAddress= "sese.com";}Just like AppStart.cshtml, there is a “_PageStart.cshtml” standard file that runs every time a page is executed. Database access ASP.NET Web Pages and WebMatrix toolset provides you an easier way to access and read data from a database. You can open and browse data from a database in your HTML page as shown below : @{ var DB = Database.Open("sertanyaman");} @{ var result = DB.Query("SELECT id, name from people");} Id Name @foreach(var row in result){ @row.Id @row.Name } @{ DB.Close();}Helpers Helpers are just server side functions that can output a visual part into your page using given parameters. You can use one of the Razor built-in helpers (visit Razor API quick reference to see a list of them) or create custom helpers for yourself to render repeating blocks of HTML in your page. To declare your custom helper, first you need to create a new cshtml file under the “App_Code” folder of ASP.NET then insert your helper inside the cshtml like below : @helper DrawBanner(string picture, string link){ > }
ASP.NET Web Pages (Razor) API Quick Reference | Microsoft Docs ASP.NET Web Pages | Microsoft Docs ASP.NET Razor – Markup | W3 Schools Источник: http://devblog.sertanyaman.com/2018/...asp-net-razor/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору. |
|
06.04.2018, 05:12 | #2 |
Участник
|
О какой пост, видно товарищ уже задумался что пора валить с AX
|
|
06.04.2018, 06:47 | #3 |
Участник
|
Он о себе пишет "I am a software developer from Netherlands who mostly works with Dynamics AX, .NET and Web programming since year 1999.", что как-бы намекает, что человек имеет более широкую сферу деятельности.
|
|
|
Опции темы | Поиск в этой теме |
Опции просмотра | |
|