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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:31:24+00:00 2026-05-29T10:31:24+00:00

I want to create a simple extension of HtmlHelper.ActionLink that adds a value to

  • 0

I want to create a simple extension of HtmlHelper.ActionLink that adds a value to the route value dictionary. The parameters would be identical to HtmlHelper.ActionLink, i.e.:

public static MvcHtmlString FooableActionLink(
    this HtmlHelper html,
    string linkText,
    string actionName,
    string controllerName,
    object routeValues,
    object htmlAttributes)
{
  // Add a value to routeValues (based on Session, current Request Url, etc.)
  // object newRouteValues = AddStuffTo(routeValues);

  // Call the default implementation.
  return html.ActionLink(
      linkText, 
      actionName, 
      controllerName, 
      newRouteValues, 
      htmlAttributes);
}

The logic for what I am adding to routeValues is somewhat verbose, hence my desire to put it in an extension method helper instead of repeating it in each view.

I have a solution that seems to be working (posted as an answer below), but:

  • It seems to be unnecessarily complicated for such a simple task.
  • All the casting strikes me as fragile, like there are some edge cases where I’m going to be causing a NullReferenceException or something.

Please post any suggestions for improvement or better solutions.

  • 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-29T10:31:25+00:00Added an answer on May 29, 2026 at 10:31 am
    public static MvcHtmlString FooableActionLink(
        this HtmlHelper html,
        string linkText,
        string actionName,
        string controllerName,
        object routeValues,
        object htmlAttributes)
    {
        // Convert the routeValues to something we can modify.
        var routeValuesLocal =
            routeValues as IDictionary<string, object>
            ?? new RouteValueDictionary(routeValues);
    
        // Convert the htmlAttributes to IDictionary<string, object>
        // so we can get the correct ActionLink overload.
        IDictionary<string, object> htmlAttributesLocal =
            htmlAttributes as IDictionary<string, object>
            ?? new RouteValueDictionary(htmlAttributes);
    
        // Add our values.
        routeValuesLocal.Add("foo", "bar");
    
        // Call the correct ActionLink overload so it converts the
        // routeValues and htmlAttributes correctly and doesn't 
        // simply treat them as System.Object.
        return html.ActionLink(
            linkText,
            actionName,
            controllerName,
            new RouteValueDictionary(routeValuesLocal),
            htmlAttributesLocal);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

JavaScript. I want to create simple script, that will be resize loaded image using
I want to create a simple app that shows me the city of the
I want to create a simple extension which modifies text buffers based on a
I am trying to create a simple javascript based extension for Google Chrome that
I want to create simple app able to edit images. Main view of app
I want to create a simple app, where the user can take a photo.
I want to create a simple Javascript program with a HTML interface. The program
I want to create a simple server application which runs on the desktop, and
I want to create a simple login functionality in WP7 app using remote MySQL
I want to create a simple bash script to launch a Java program on

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.