AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 05.04.2018, 17:11   #1  
Blog bot is offline
Blog bot
Участник
 
25,617 / 848 (80) +++++++
Регистрация: 28.10.2006
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:
  • @foreach (var record in queryResult) {
  • @record.id }
@if (i>30){Too many tries.}else{Try once again.}It is possible to use other .NET libraries inside a Razor script block, but Razor also has its own specific function set for easier access to some functions. The IsPost, RenderPage functions in the code blocks I gave above are some examples to this function set. Additionally you can declare your own custom functions to be used in your document by adding a .cs file under the ASP.NET folder “App_Code”.

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){ >
}
  • @CustomHelpers.DrawBanner("~/Images/pic.jpg", "Yet another banner")
For more information on Razor and ASP.NET Wep Pages programming model check the links below :

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  
trud is offline
trud
Участник
Лучший по профессии 2017
 
1,039 / 1630 (57) ++++++++
Регистрация: 07.06.2003
Записей в блоге: 1
О какой пост, видно товарищ уже задумался что пора валить с AX
Старый 06.04.2018, 06:47   #3  
skuull is offline
skuull
Участник
Most Valuable Professional
Лучший по профессии 2014
 
700 / 752 (27) +++++++
Регистрация: 08.03.2013
Адрес: ХЗ
Он о себе пишет "I am a software developer from Netherlands who mostly works with Dynamics AX, .NET and Web programming since year 1999.", что как-бы намекает, что человек имеет более широкую сферу деятельности.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
alexef: Visual Studio (C#): ASP.NET Core Web API app Blog bot NAV: Blogs 0 04.04.2018 21:11
atinkerersnotebook: Walkthrough & Tutorial Summary Blog bot DAX Blogs 1 09.09.2013 09:11
fed: Net requirements update in MRP Module and Working Set of MRP Blog bot DAX Blogs 14 08.05.2012 13:09
german_nav_developer: Buildnummern-Übersicht Microsoft Dynamics NAV 2009 SP1 Blog bot Dynamics CRM: Blogs 0 11.06.2010 16:33
german_nav_developer: Buildnummern-Übersicht Microsoft Dynamics NAV 2009 Blog bot Dynamics CRM: Blogs 0 04.06.2010 13:21

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 04:58.