Appframe Knowledge Base


11 hits
4

Miscellaneous razor functions

Site language: @Language → EN
Localize string: @Localize("hello") → hello
Breadcrumb: @Breadcrumb() → Web > Web 4 > Razor documentation
Link to another page on the site: @Link("login") → Article not found: login
Start a HTML element: @Begin("li") → 
End a HTML element: @End("li") → 
HTML-encode a string: @HtmlEncode("<") → <
URL-encode a string: @UrlEncode("?") → "%3f"
Javascript-encode a string: [" '] → "\" and \u0027"

User-info: Login → @User.ShortName
                Name → @User.FullName
                Authenticated → @User.IsAuthenticated

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)

1

Execute a stored procedure and output html for each row in razor

@{
	var vProcedureCall = ProcedureCall("astp_Some_Procedure");
	var vDataSet = vProcedureCall.Execute();
}

<ul>
@foreach (var vRow in vDataSet[0])
{
	<li>@HtmlEncode(vRow["Column1"])</li>
}
</ul>

razor web · Perma link post comment Posted by: Peter Øren (15-jan-2013)

Arild Røksund :
Should vCategories be vDataSet ? (17-feb-2015)
Peter Øren :
Yes (23-feb-2015)
0

Access current article meta info in razor

Article ID: @Article.Id → razor
Parent ID: @Article.ParentId → web4
Title: @Article.Title → Razor documentation
Author: @Article.Author → Kvassen, Ola
Summary: @Article.Summary →
List of files: @Article.Files

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)

0

Search a record source using a query parameter and output html for each row in razor

@{
	var vRecordSource = RecordSource("aviw_Some_View");
	string vSearch = @QueryString.RawValue("query");
	vRecordSource.SelectColumns.Add("Column1");
	if(vSearch != null && vSearch != ""){
		vRecordSource.WhereClause = "SearchColumn LIKE '%" + vSearch.Replace("'", "''") + "%'";
	}
	vRecordSource.OrderByColumns.Add("Column1",eDescAsc.Asc);
	vRecordSource.MaxRecords = -1;
	var vData = vRecordSource.GetData();
}
@if(vData.Count > 0){
	<ul>
	@foreach (var vRow in vData)
	{
		<li>@HtmlEncode(vRow["Column1"].ToString())</li>
	}
	</ul>
}

razor web · Perma link post comment Posted by: Peter Øren (15-jan-2013)

0

Retrieve URL/Querystring in Razor

// Remember to put this inside a @{ } block, so it does not just get rendered out as text
string vUrl = System.Uri.UnescapeDataString(RequestUrl.AbsolutePath);
string[] vUrlParts = vUrl.Split('/');

razor web · Perma link post comment Posted by: Peter Øren (11-nov-2013)

0

How to get and use data from a data source in Razor

// case sensitive stuff!

@{ var data = GetRows("dsMyDatasource", "MyField = 'Something'"); }

@data.Count <!-- Returns the number of rows contained in 'data' -->
@data.Sum("FieldName") <!-- Return the sum of all the values in the specified column -->
@data.Min("FieldName") <!-- Return the smallest of all the values in the specified column -->
@data.Max("FieldName") <!-- Return the largest of all the values in the specified column -->
@data.Average("FieldName") <!-- Return the average of all the values in the specified column -->

@foreach(dynamic row in table){
    <!-- The dynamic keyword makes it possible to access fields on the row 
         without using square brackets ( ["FieldName"] ) -->

    <!-- Return the typed value of a field -->
    @row["FieldName"] 
    @row.FieldName
    
    <!-- Write the value of a field using the default format or using a specified format string -->
    @row.Write("FieldName")
    @row.Write("FieldName", "dd-MMM-yyyy")
}

<!-- Group the data by one or more fields by using .GroupBy(ParamArray pFieldName() As String) -->
@foreach(dynamic group in table.GroupBy("FieldA", "FieldB")){
    <!-- The dynamic keyword makes it possible to access the values of the grouped fields
        without using square brackets ( ["FieldName"] ) -->
 
     <!-- Return the typed value of one of the grouped fields -->
    @group["FieldA"] 
    @group.FieldB
    
    @foreach(dynamic row in group) {
        <!-- Lets you do the same as you normally do with rows -->
    }
    
    @group.Count <!-- eturns the number of rows in the group -->
    @group.Sum("FieldName") <!-- Return the sum of all the values in the specified column -->
    @group.Min("FieldName") <!-- Return the smallest of all the values in the specified column -->
    @group.Max("FieldName") <!-- Return the largest of all the values in the specified column -->
    @group.Average("FieldName") <!-- Return the average of all the values in the specified column -->
    
    <!-- Further group the data by another field by using .GroupBy(ParamArray pFieldName() As String) -->
    @foreach(dynamic subGroup in group.GroupBy("FieldC")){
        <!-- subGroup is used in the exact same manner as group -->
    }
}

razor web · Perma link post comment Posted by: Sigmund Lunde (11-nov-2013)

0

Numeric comparisons

@Where.GreaterThan("TimeZoneCode", "x").GetRows("dsUsers");
<!-- Resulting filter: [TimeZoneCode] < @parameter -->

@Where.GreaterThanOrEqual("TimeZoneCode", "x").GetRows("dsUsers");
<!-- Resulting filter: [TimeZoneCode] <= @parameter -->

@Where.LessThan("TimeZoneCode", "x").GetRows("dsUsers");
<!-- Resulting filter: [TimeZoneCode] > @parameter -->

@Where.LessThanOrEqual("TimeZoneCode", "x").GetRows("dsUsers");
<!-- Resulting filter: [TimeZoneCode] >= @parameter -->

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)

0

String comparisons

@Where.Contains("Login", "x").GetRows("dsUsers");
<!-- Resulting filter: [Login] LIKE '%' + @parameter + '%' -->
@Where.IsEqual("Login", "x").GetRows("dsUsers");
<!-- Resulting filter: [Login] = @parameter -->

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)

0

Security considerations when using @QueryString in razor

<ul> <li>@QueryString.RawValue() gives UNESCAPED querystring parameters and opens for possible XSS! <li>When using the RawValue accessor, always check using a regular expression! <li>Should always be escaped when used in Razor: @HtmlEncode(@QueryString.RawValue("x")) → <li>Best practice: Use "verifying" accessors <li>Integer: @QueryString.IntValue("parameter") - Digits <li>Decimals: @QueryString.DecimalValue("parameter") - Digits followed by optional period and more digits <li>Alphanumeric: @QueryString.Alphanumeric("parameter") - A-Z, a-z and 0-9 <li>List: @QueryString.List("parameter") - Alphanumeric characters and comma <li>Best practice: The verifying accessors throw exceptions upon invalid values, so use checks described above before using them </ul>

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)

0

Checking for multiple things in QueryString in one go

@QueryString.ContainsAll("x", "y", "z")
<!-- Returns whether or not all of the specified query parameters are supplied -->

@QueryString.ContainsIntegers("x", "y", "z")
<!-- Returns whether or not all of the specified query parameters are integers -->

@QueryString.ContainsDecimals("x", "y", "z")
<!-- Returns whether or not all of the specified query parameters are decimals -->

@QueryString.ContainsAlphanumerics("x", "y", "z")
<!-- Returns whether or not all of the specified query parameters are alphanumeric -->

@QueryString.ContainsLists("x", "y", "z")
<!-- Returns whether or not all of the specified query parameters are lists -->

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)

0

Checking if QueryString contains a parameter of a specific type

@QueryString.ContainsInteger("x")
<!-- Returns whether or not the specified query parameter is an integer -->

@QueryString.ContainsDecimal("y")
<!-- Returns whether or not the specified query parameter is a decimal number -->

@QueryString.ContainsAlphanumeric("z")
<!-- Returns whether or not the specified query parameter is alphanumeric -->

@QueryString.ContainsList("z")
<!-- Returns whether or not the specified query parameter is a list -->

@QueryString.Contains("x", "^\w+\\w+\.\w+$")
<!-- Returns whether or not the specified query parameter matches the specified regex -->

razor web · Perma link post comment Posted by: Peter Øren (23-aug-2013)