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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:09:47+00:00 2026-05-26T15:09:47+00:00

I have an extension method for getting a string from resource file in asp.net

  • 0

I have an extension method for getting a string from resource file in asp.net mvc 3

public static string Resource(this HtmlHelper htmlHelper, string expression, params object[] args)
        {

            string path = ((RazorView)htmlHelper.ViewContext.View).ViewPath;
            var fields =
                (ResourceExpressionFields)
                (new ResourceExpressionBuilder()).ParseExpression(expression, typeof(string), new ExpressionBuilderContext(path));
            return (!string.IsNullOrWhiteSpace(fields.ClassKey))
                       ? string.Format((string)htmlHelper.ViewContext.HttpContext.GetGlobalResourceObject(
                           fields.ClassKey,
                           fields.ResourceKey,
                           CultureInfo.CurrentUICulture), args)
                       : string.Format((string)htmlHelper.ViewContext.HttpContext.GetLocalResourceObject(
                           path,
                           fields.ResourceKey,
                           CultureInfo.CurrentUICulture), args);
        }

Here is how I use this method

@Html.LabelFor(m => m.Login, Html.Resource("LoginBoxLoginField")) 

But I don’t know how to use it in an action of controller

public ActionResult MyAction()
        {
             //how to get string from resource file using my extension method?
        }

For example

 if (!ModelState.IsValid)
    ModelState.AddModelError(string.Empty, string.Empty/* should be resource string */);
  • 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-26T15:09:47+00:00Added an answer on May 26, 2026 at 3:09 pm

    You cannot use it in the controller because you do not have an instance of HtmlHelper inside the controller. And to create an HtmlHelper instance you need a ViewContext. And you have a view context only inside a view. If you want to use this in a controller you will have to refactor it so that this helper depends only on the HttpContext and not on an HtmlHelper.

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

Sidebar

Related Questions

I have Ninject configured to do DI in an ASP.NET MVC 3 application. This
i have this model on mvc: public class User { public string Name {
I have an extension method on the HttpApplicationState object for getting my IoC container
I have an extension method which I can use from the .cs codebehind of
I have written an extension method for string manipulation. I'm confused what should I
I have a HtmlHelper extension method that I would like to apply some logic
I have an extension method to dynamically filter Linq to Entities results using string
Hi I have converted this parallel extension c# code sample to VB.NET http://code.msdn.microsoft.com/Samples-for-Parallel-b4b76364/sourcecode?fileId=25353&pathId=215900242 using
In my asp.net MVC 3 application, I have nested layouts. I have followed following
i have wrote a extension method for customize my validation messages, like this: namespace

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.