Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor function: What is the difference between @helper and @functions

Few question about razor function in asp.net mvc.

1) see the code below

@helper WelcomeMessage(string username)
{
    <p>Welcome, @username.</p>
}

Then you invoke it like this: @WelcomeMessage("John Smith")

@functions{
    public string GetSomeString(){
        return string.Empty;
    }
}

see there is two razor function. in one @helper is used to declare razor function and in second one @functions. so tell me what is the difference between @helper and @functions ?

2) can we declare razor function in .cs code...if yes then is there any convention we need to follow ?

3) can we return integer from razor function

@helper Calculator(int a, int b)
{
    @{
        var sum = a + b;
    }
    <b>@sum</b>
}

@Calculator(1, 2)

can we return sum to its calling environment?

like image 713
Monojit Sarkar Avatar asked Feb 08 '18 09:02

Monojit Sarkar


People also ask

What is the difference between tag helper vs HTML helper?

Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.

What is @section in Razor?

@section is for defining a content are override from a shared view. Basically, it is a way for you to adjust your shared view (similar to a Master Page in Web Forms).

What is Razor syntax in C#?

Razor is a markup syntax for embedding . NET based code into webpages. The Razor syntax consists of Razor markup, C#, and HTML. Files containing Razor generally have a . cshtml file extension.

How do you define a function in razor view?

If you want to write a function, you can't just open new @{ } razor block and write it there… it won't work. Instead, you should specify @functions { } so that inside the brackets you will write your own C#/VB.NET functions/methods. This code will print the numbers from 1 to 10: Number 1.


2 Answers

Both are for the purpose of Reusability.

But @functions are used when there is no html needed to be returned back and we only want to do some calculations or some business logic which means we need to write purely C# code.

For @functions we could use them when we don't want to return html back in the view. If we want to reutrn Html from the @functions we will need to specifically return HtmlString from it instead of String and for @functions we will also need to specify and include the namespaces in it if we want to return HtmlString like:

@using System.Web.Mvc;
@functions {

   public static HtmlString WelcomeMessage(string username)
   {

       return new HtmlString($"<p>Welcome, {username}.</p>");
   }
}

And @helper is useful when we want to create html and render it with some logic which means that we need to write razor code.

For @helper they are used when our method we are defining needs to be mixed with Html and we want to return some html back.

@helper{

   public WelcomeMessage(string username)
   {

       <p>Welcome, @username.</p>;
   }
}

Please read the following great post which explains in detail the differences of both:

https://www.mikesdotnetting.com/article/173/the-difference-between-helpers-and-functions-in-webmatrix

like image 144
Ehsan Sajjad Avatar answered Oct 12 '22 05:10

Ehsan Sajjad


You can place helper code in cs as External HTML helper inside some static helper class:

public static class ExternalHelper
{
    public static MvcHtmlString Sum(this HtmlHelper htmlHelper, int[] items)
    {
        return new MvcHtmlString(items.ToArray<int>().Sum().ToString());
    }
}

and use it in View

@Html.Sum(new int[] { 1, 3,7 })

Edit: don't forget to place that static helper class namespace under Views/Web.config section

<add namespace="ProjectNamespace.Helpers" />
like image 43
Zoran Bosnjak Avatar answered Oct 12 '22 05:10

Zoran Bosnjak