How do I define a method in Razor?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- MVC-Routing,Why i can not ignore defaults,The matc
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
You can also just use the
@{ }
block to create functions:Then later in your razor page:
Leaving alone any debates over when (if ever) it should be done, @functions is how you do it.
You mean inline helper?
It's very simple to define a function inside razor.
So you can call a the function anywhere. Like
However, this same work can be done through
helper
too. As an exampleSo what is the difference?? According to this previous post both @helpers and @functions do share one thing in common - they make code reuse a possibility within Web Pages. They also share another thing in common - they look the same at first glance, which is what might cause a bit of confusion about their roles. However, they are not the same. In essence, a helper is a reusable snippet of Razor sytnax exposed as a method, and is intended for rendering HTML to the browser, whereas a function is static utility method that can be called from anywhere within your Web Pages application. The return type for a helper is always HelperResult, whereas the return type for a function is whatever you want it to be.
You can simply declare them as local functions in a razor block (i.e.
@{}
).Razor is just a templating engine.
You should create a regular class.
If you want to make a method inside of a Razor page, put them in an
@functions
block.