How to create a function in a cshtml template?

Saeed Neamati picture Saeed Neamati · Jun 30, 2011 · Viewed 136k times · Source

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.

Answer

user156888 picture user156888 · Jun 30, 2011

why not just declare that function inside the cshtml file?

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

<h2>index</h2>
@GetSomeString()