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

  • SEARCH
  • Home
  • 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 7404787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:18:21+00:00 2026-05-29T05:18:21+00:00

I have too much text utility methods, like MakeShortText(string text, int length) , RemoveTags(string

  • 0

I have too much text utility methods, like MakeShortText(string text, int length), RemoveTags(string text), TimeAgo(DateTime date) and other.
I want to access them from separate helper like in next example:

@Text().MakeShortText(Model.Text, 10)

Is it possible to create such extension? Or I have to make extension for HtmlHelper like this:

@Html.Text().MaksShortText(Model.Text, 10)

?

  • 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-29T05:18:22+00:00Added an answer on May 29, 2026 at 5:18 am

    You could start by defining a custom TextHelper:

    public class TextHelper
    {
        public TextHelper(ViewContext viewContext)
        {
            ViewContext = viewContext;
        }
    
        public ViewContext ViewContext { get; private set; }
    }
    

    and then have all your methods be extension methods of this TextHelper:

    public static class TextHelperExtensions
    {
        public static IHtmlString MakeShortText(
            this TextHelper textHelper, 
            string text,
            int value
        )
        {
            ...
        }
    }
    

    Then you could define a custom web page:

    public abstract class MyWebViewPage<T> : WebViewPage<T>
    {
        public override void InitHelpers()
        {
            base.InitHelpers();
            Text = new TextHelper(ViewContext);
        }
    
        public TextHelper Text { get; private set; }
    }
    

    then in your ~/Views/web.config (not in ~/web.config) configure this custom web page as base page for your Razor views using the pageBaseType attribute:

    <pages pageBaseType="AppName.Web.Mvc.MyWebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <add namespace="System.Web.Mvc.Ajax" />
            <add namespace="System.Web.Mvc.Html" />
            <add namespace="System.Web.Routing" />
        </namespaces>
    </pages>
    

    and then in your views you will be able to use:

    @Text.MakeShortText(Model.Text, 10)
    

    And if you wanted to use the following syntax as shown in your question:

    @Text().MakeShortText(Model.Text, 10)
    

    simply modify the custom view engine so that Text is not a property but a method that will return an instance of the TextHelper. Or even return the HtmlHelper instance so that you don’t need to move your existing extension methods to TextHelper:

    public abstract class MyWebViewPage<T> : WebViewPage<T>
    {
        public HtmlHelper Text()
        {
            return Html;
        }
    }
    

    The second syntax is also possible:

    @Html.Text().MaksShortText(Model.Text, 10)
    

    Simply define a custom Text() extension for the HtmlHelper class:

    public static class HtmlExtensions
    {
        public static TextHelper Text(this HtmlHelper htmlHelper)
        {
            return new TextHelper(htmlHelper.ViewContext);
        }
    }
    

    and then the same way as in the first case you would have your custom methods be extension methods of this TextHelper class.

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

Sidebar

Related Questions

Q1: Is there something like too much ajax?? Explanation: I have been seeing programmers
I have written a tool tip snippet, its throwing error like TOO MUCH RECURSION
I have code blindness, it's like snowblindness, just wth far too much code. I
I don't have too much experience with C# so if someone could point me
I have spent too much time on this problem and am beginning to think
I have text in db which looks like this: <p> blah blah blah </p>
I have a fluid layout, but if the browser window is stretched too much,
I have done some web based projects, but I don't think too much about
I have a selection of text that looks like the following. I need to
I don't have too much experience with VB Scripting but I'm trying to write

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.