Creating Methods with Parameter in Razor Syntax

In this chapter you will learn
  • How to create methods with parameter in razor syntax?
  • Programming Example

How to create methods with parameter in razor syntax?

You can simply create methods in razor syntax using @functions keyword. See the following example for better demonstration.

Programming Example

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        @functions{
            public int sum(int x, int y)
            {
                return x+y;
            }
        }
        <h3>Add of 5 + 6 is @sum(5,6)</h3>
        <h3>Add of 9 + 10 is @sum(9,10)</h3>
    </body>
</html>
  Output
Add of 5 + 6 is 11 Add of 9 + 10 is 19

Summary

In this chapter you learned to declare or define methods with parameter in razor syntax. In the next chapter you will learn to handle exceptions and errors in razor syntax.
 

Share your thought