I need to create a function that is only necessary inside one cshtml file. You can think of my situation as ASP.NET page methods, which are min web services implemented in a page, because they're scoped to one page. I know about HTML helpers (extension methods), but my function is just needed in one cshtml file. I don't know how to create a function signature inside a view. Note: I'm using Razor template engine.
Asked
Active
Viewed 1.7e+01k times
248
-
See also [The Difference Between Helpers and Functions](https://www.mikesdotnetting.com/article/173/the-difference-between-helpers-and-functions-in-webmatrix) – Michael Freidgeim Jan 06 '21 at 11:31
6 Answers
469
why not just declare that function inside the cshtml file?
@functions{
public string GetSomeString(){
return string.Empty;
}
}
<h2>index</h2>
@GetSomeString()
-
37This should be marked as the answer, as the @functions directive specifically meets the OP requirements. The Helpers feature is intended for shared use across multiple template files by putting the file with the @helper directive into an App_Code directory. Whereas, the @functions directive allows a function to be used only by the template that declares it. – Jon Davis Jul 04 '11 at 21:24
-
I didn't want to say anything but the OP did say "but my function is just needed in one cshtml file" - my answer obv does exactly that where as the marked answer does not. – Jul 05 '11 at 08:00
-
@stimpy77 Can you provide a source for "The Helpers feature is _intended_ for shared use across multiple template files"? See [ASP.NET MVC 3 and the helper syntax within Razor](http://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx) in ScottGu's Blog. – Daniel Liuzzi Jul 28 '11 at 17:25
-
1o.O your own link is rather explicit. "Reusing helpers across multiple views" "we can define the helper method outside of our view template, and enable it to be re-used across all of the view templates in our project" blah blah blah – Jon Davis Jul 29 '11 at 05:09
-
@stimpy77 Let's try that again: "**Alternatively**, we can define the Helper method outside of our view template..." (ScottGu) - "The Helpers feature is **intended** for shared use across multiple template..." (you) The "Alternatively" part (that you conveniently left out of Scott's quote) and the "intended" part (that you unnecessarily added to yours) are the key. We normally use eggs for cooking. The fact that we **can alternatively** use them for going egging doesn't mean that they are **intended** for it, or that we shouldn't use eggs for making breakfast. – Daniel Liuzzi Aug 03 '11 at 14:09
-
Daniel, you argue the semantics of "alternatively" but I find it highly doubtful that ScottGu has a background in being a lawyer. He was clearly taking a step-by-step tutorial approach to instruction of best practices, leading up to the end goal. Everything about the article appears to me to be formatted to suggest that the whole point of the feature is to enable its reuse. Futher, semantically, a "helper" is in fact a utility feature that is by definition intended to be reused, not a function to be used in an isolate context. I leave you to your opinion and ask that we agree to disagree. – Jon Davis Aug 03 '11 at 18:31
-
i agree with stimpy77 - i also agree that the OP had asked for a way to show a means to have a"function that is only necessary inside one cshtml file". if you have globally available methods that aren't needed outside of the scope of a single use - then your API will be rendered unusable. – Aug 03 '11 at 20:16
-
7Also note helpers seem oriented to returning strings just like other razor helpers already do, and thus the `functions` solution provides more flexibility for toher return types. Both answers get +1 in my book though as they are both useful tidbits of info. – AaronLS Oct 24 '11 at 21:46
-
@AaronLS Agreed. Using functions also gives you the option of making functions private, unlike helpers. – Ben Sutton Feb 17 '12 at 19:11
-
9@AaronLS To be fair, helpers don't return strings but IHtmlString, which take care of HTML encoding for you and protect your app from XSS attacks. Helpers also give you the convenience of Razor syntax in the helper itself, which you lose with functions. In other words, `
Welcome, @username.
` versus `return new HtmlString("Welcome, " + Html.Encode(username) + ".
");`. – Daniel Liuzzi Feb 21 '12 at 05:14 -
@Daniel +1 Good to know. There definitely isn't a best. Each provides a different type of flexibility depending on what you want to accomplish. – AaronLS Feb 21 '12 at 15:51
-
9using `@helper` in a single view doesn't make it available to other views, though. the reason I like @helper better is you can put html between your curly braces. `@functions` doesn't (easily) let you do that. – jfren484 Oct 23 '13 at 15:11
-
5Just for the record: both `@helper` and `@functions` can be shared among many views, and both can be declared into and used by a single view (and I have personally found use for them in both shared/single scenarios). IMHO the only practical difference between them is the fact that a view helper adds syntactic sugar for returning rendered HTML snippets (or, more appropriate, `HelperResult` instances), while a view function is *usually* only useful for returning simple reference or value types. – rsenna Jan 24 '14 at 17:48
-
-
@GeorgiKovachev put them in the App_Code directory and they are globaly available – Dec 11 '16 at 10:02
310
You can use the @helper Razor directive:
@helper WelcomeMessage(string username)
{
<p>Welcome, @username.</p>
}
Then you invoke it like this:
@WelcomeMessage("John Smith")

Daniel Liuzzi
- 16,807
- 8
- 52
- 57
-
14You can't put tags inside the `@functions` methods, so I like this answer. – jfren484 Oct 23 '13 at 15:11
-
1Yes this is much better than declaring a function. Much more straight forward. – muglio Jun 05 '15 at 06:37
-
2
-
-
I was just commenting that I don't think @helper could return a variable. It just renders markup/razor. When I read OP's post I thought he needed to return a variable but maybe not. – Paul Jun 21 '16 at 13:38
-
Got it. You mean `@helper` cannot return values other than IHtmlString. That is correct. @helper is like a specialized function which is optimized for returning HTML (i.e. it does HTML encoding automatically, whereas plain functions do not.) – Daniel Liuzzi Jun 21 '16 at 14:10
-
4
-
3@MuM6oJuM6o no it doesn't. You can find alternatives listed in this article [What Happened To Helper In ASP.NET Core?](https://www.mikesdotnetting.com/article/344/what-happened-to-helpers-in-asp-net-core) – Bruno Martins Mar 12 '20 at 14:16
-
1**For aspnet core**, see [Migrate html helpers to ASP.NET Core](https://stackoverflow.com/q/42494980/1366033) or the other answers in this post – KyleMit Aug 22 '22 at 18:29
29
If your method doesn't have to return html and has to do something else then you can use a lambda instead of helper method in Razor
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
Func<int,int,int> Sum = (a, b) => a + b;
}
<h2>Index</h2>
@Sum(3,4)

Muhammad Hasan Khan
- 34,648
- 16
- 88
- 131
-
It is useful though if you need to access page's global variables in your function, is there an other way to do so ? :/ – Alexandre Daubricourt Feb 16 '19 at 12:07
7
In ASP.NET Core Razor Pages, you can combine C# and HTML in the function:
@model PagerModel
@{
}
@functions
{
void PagerNumber(int pageNumber, int currentPage)
{
if (pageNumber == currentPage)
{
<span class="page-number-current">@pageNumber</span>
}
else
{
<a class="page-number-other" href="/table/@pageNumber">@pageNumber</a>
}
}
}
<p>@PagerNumber(1,2) @PagerNumber(2,2) @PagerNumber(3,2)</p>

Petr Voborník
- 1,249
- 1
- 14
- 11
2
If you want to access your page's global variables, you can do so:
@{
ViewData["Title"] = "Home Page";
var LoadingButtons = Model.ToDictionary(person => person, person => false);
string GetLoadingState (string person) => LoadingButtons[person] ? "is-loading" : string.Empty;
}

Alexandre Daubricourt
- 3,323
- 1
- 34
- 33
-
Since C# 7.0 this is the only proper and correct answer. `GetLoadingState()` here is local function. – Wolfrevok Cats Apr 26 '20 at 21:44