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 5999351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:32:23+00:00 2026-05-23T00:32:23+00:00

In this post I wondered about cleaner code when internationalising an app. that leads

  • 0

In this post I wondered about cleaner code when internationalising an app. that leads to this second query… supposing I wanted to call a function like this:

@Html.RenderWithTags("Help",
    new Dictionary<string, string>() { "HelpPage", "@Html.ActionLink(...)" }
)

such that I look up a string in my local resource file containing embedded “tags” e.g. resource name “Help” contains:

We suggest you read our [HelpPage]
before proceeding

and then my .RenderWithTags() method will expand the tags but dynamically executing the code in the dictionary passed e.g. replace [HelpPage] with whatever @Html.ActionLink(...) produces.

I know I can use Microsoft.CSharp.CSharpCodeProvider().CreateCompiler() to compile C# code on the fly, but what about Razor code?

  • 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-23T00:32:24+00:00Added an answer on May 23, 2026 at 12:32 am

    in the end, the solution turned out pretty slick. would not have been possible without SLaks, I’m much obliged for the help (though I didn’t end up using inline helpers (but thanks for the intro (they’re very cool))).
    Now my page contains this:

    @{
        Dictionary<string, MvcHtmlString> tokenMap = new Dictionary<string, MvcHtmlString>() {
            {"HelpPage", Html.ActionLink("help page", "Help", "Home") }
        };
    }
    

    and somewhere below I have:

    @this.Resource("Epilogue", tokenMap)
    

    To accomplish this simplicity:

    public static class PageExtensions
    {
        public static MvcHtmlString Resource(this WebViewPage page, string key)
        {
            HttpContextBase http = page.ViewContext.HttpContext;
            string ret = (string) http.GetLocalResourceObject(page.VirtualPath, key);
            return MvcHtmlString.Create(ret);
        }
    
        public static MvcHtmlString Resource(
            this WebViewPage page, string key, 
            Dictionary<string, MvcHtmlString> tokenMap
        ) {
            HttpContextBase http = page.ViewContext.HttpContext;
            string text = (string) http.GetLocalResourceObject(page.VirtualPath, key);
            return new TagReplacer(text, tokenMap).ToMvcHtmlString();
        }
    }
    

    …and:

    public class TagReplacer
    {
        Dictionary<string, MvcHtmlString> tokenmap;
        public string Value { get; set; } 
    
        public TagReplacer(string text, Dictionary<string, MvcHtmlString> tokenMap)
        {
            tokenmap = tokenMap;
    
            Regex re = new Regex(@"\[.*?\]", RegexOptions.IgnoreCase);
            Value = re.Replace(text, new MatchEvaluator(this.Replacer));
        }
    
        public string Replacer(Match m)
        {
            return tokenmap[m.Value.RemoveSet("[]")].ToString();
        }
    
        public MvcHtmlString ToMvcHtmlString()
        {
            return MvcHtmlString.Create(Value);
        }
    }
    

    …with a little extra help:

    public static class ObjectExtensions
    {
        public static string ReplaceSet(this string text, string set, string x)
        {
            for (int i = 0; i < set.Length; i++)
            {
                text = text.Replace(set[i].ToString(), x);
            }
            return text;
        }
        public static string RemoveSet(this string text, string set)
        {
            return text.ReplaceSet(set, "");
        }
    }
    

    comments or feedback on how it could have been better most welcome!

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

Sidebar

Related Questions

This is something I've wondered about in a couple of frameworks that I've messed
So this post talked about how to actually implement url rewriting in an ASP.NET
I have read this post about how to test private methods. I usually do
I just read this post about why new-line warnings exist, but to be honest
There is an interesting post over here about this, in relation to cross-application flow
High! I just wondered why this won't work? $.post($(#jsCheckoutForm_1b).attr(action), { sLoginName: $(#sLoginName).val(), sPassword: $(#sPassword).val()
This could easily be done using code, but I wondered if it could be
this post is a long one sorry for that but the problem is complex
This post says that you need a 64 bit system for MongoDB if your
Ive always wondered about this. I like to create super navigation menus. The website

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.