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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:51:14+00:00 2026-05-26T06:51:14+00:00

I want that in my action method, my Request.Url.Host should be available. However I

  • 0

I want that in my action method, my Request.Url.Host should be available. However I don’t want to set it in routedata because it will be visible to the user in the url. How can I accomplish this such that Request.Url.Host is available in my domain name variabele but now shown to the user in url? I tried adding it to Request.Form.Add(“domain”,Request.Url.Host) but it doesn’t work because Request.Form is readonly collection.

eg:

public ActionResult Foo(string domain, int id) //id is the standard routedata that you get from url.
{
    ..//code
}

What is the best I can do? I don’t want to do Request.Url.Host in every action method so I am trying this approach.

I tried to do this:

public class MyRoute : Route
    {
        #region '----- Method(s) -----'
        public MyRoute(string url, object defaults)
            : base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
        { }

        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData routeData = base.GetRouteData(httpContext);

            string domain = httpContext.Request.Url.Host;

            routeData.DataTokens["domain"] = domain;

            return routeData;
        }
        #endregion
    }

But my domain variable in my action method comes null.

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-26T06:51:15+00:00Added an answer on May 26, 2026 at 6:51 am

    EDIT: Complete rewrite:

    You should add your custom data to routeData.Values instead of routeData.DataTokens.

    public class MyRoute : Route
    {
        #region '----- Method(s) -----'
        public MyRoute(string url, object defaults)
            : base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
        { }
    
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData routeData = base.GetRouteData(httpContext);
    
            string domain = httpContext.Request.Url.Host;
    
            routeData.Values["domain"] = domain;
    
            return routeData;
        }
        #endregion
    }
    

    And registering your MyRoute route (MVC uses the first matching route so it should go first):

    routes.Add(new MyRoute("{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to have a route that looks something like: www.abc.com/companyName/Controller/Action/Id However, all the
I want to forward request to a non-JSF page from JSF action method. I
I have two action methods that are conflicting. Basically, I want to be able
I've an IBAction that calls up a panel. I want to invoke that action
I am using the SparkViewEngine and I am calling an action that I want
I have code that I want to be executed before every action is called.
I want to create a logout action in JSPs such that there is no
I want to create an application that performs a particular action when the device
I have an action which create QMessageBox. In that dialog I want to print
I want that my unit tests to cover my POCO's. How should I test

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.