Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:02:21+00:00 2026-05-25T20:02:21+00:00

Is it possible to declare a function or helper in a Razor layout view

  • 0

Is it possible to declare a function or helper in a Razor layout view and have a child Razor view use that function? My structure is :

_layout.cshtml -> index.cshtml

Where index.cshtml uses _layout.cshtml as its layout. This is the default structure.

I want to be able to place common functions/helpers in _layout.cshtml and refer to them in index.cshtml and all other views that use _layout.cshtml. It doesn’t work out of the box unless I’ve missed something.

I know. I should be using compiled AppHelpers or HtmlHelpers for this, but I can foresee a great convenience in being able to tweak select functions/helpers directly in a _layout.cshtml file.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:02:21+00:00Added an answer on May 25, 2026 at 8:02 pm

    No, that’s not possible. @functions are visible only inside the current view.

    You could use @helper which could be placed inside the App_Code folder and be reused from all views.

    For example you define ~/App_Code/MyHelpers.cshtml:

    @helper Foo()
    {
        ...
    }
    

    and then in some view:

    @MyHelpers.Foo()
    

    But I would recommend compiled helpers. They are unit test friendly and view engine agnostic and when tomorrow Microsoft reveal their brand new Blade view engine you will have 0 work to do in order to use them, whereas if you code against Razor specific things like @functions and @helper you will have to port them. Probably not a concern for you but worth mentioning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to declare a function pointer (non C++ 11) that can point
Why is not possible to initialize a property to a function when you declare
It's possible to declare variables with the below structure in C++ private: public: protected:
I have a class, in which i declare a static function. I register this
Just wondering if it is possible to declare a function of an enumerated type
What is the syntax to declare a C function that returns an Objective-C block?
Is it possible to declare a function with C linkage without it having external
Is it possible to forward-declare a function in Python? I want to sort a
How can I declare and define a function so that it is only accessible
I wonder if it's possible to write a function that returns a lambda function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.