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

The Archive Base Latest Questions

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

I need routing info for my javascript logic on the client. I have a

  • 0

I need routing info for my javascript logic on the client. I have a workable solution right now but it feels kind of clunky. I am injecting a hidden field on the page. I’m thinking there has to be a better way. This is what i’m currently doing:

<input type="hidden" id="rt" value="@string.Format("{0}://{1}{2}",Request.Url.Scheme,Request.Url.Authority,Url.Content("~"))"/>
  • 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:38:42+00:00Added an answer on May 26, 2026 at 6:38 am

    Yeah, very clunky. Actually you don’t need absolute urls for your javascript. It can work with relative urls. For example:

    @Html.Hidden("rt", Url.Content("~/"))
    

    Now whether using a hidden field is a good thing or not is though to say without knowing more details about your scenario and what exactly you are trying to do in javascript. But let’s consider a very common scenario. You want to AJAXify a form. In this case you would use HTML helpers in your view to generate the form and then:

    $(function() {
        $('#myform').submit(function() {
            $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function(result) {
                    // TODO: do something with the results
                }
            });
            return false;
        });
    });
    

    See how we no longer need any hardcoded urls or hidden fields fore the javascript.

    Or let’s consider another scenario. You want to AJAXify an anchor. Same thing. You use Html.ActionLink to generate your anchor and then:

    $(function() {
        $('#mylink').click(function() {
            $.get(this.href, function(result) {
                // TODO: do something with the results
            });
            return false;
        });
    });
    

    Or yet another scenario. You have some div container that you want to send an AJAX request to some controller action upon clicking on it. In this case you could use HTML5 data-* attributes on the div:

    <div id="mydiv" data-url="@Url.Action("Foo", "Home")">Some contents</div>
    

    and then:

    $(function() {
        $('#mydiv').click(function() {
            $.get($(this).data('url'), function(result) {
                // TODO: do something with the results
            });
            return false;
        });
    });
    

    OK, now you see that you no longer need any hidden fields. There are always possibilities to do better.

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

Sidebar

Related Questions

I need to do some special routing in cake, but can't for the life
In CakePHP, when you are using Acl/Auth, do you need to have Routing.prefixes enabled
I have the following code using normal Zend routing and I need to convert
i have a global variable who hold the value for routing i need to
I have some Cucumber features which need to interact with the Google Maps Routing
I need to add a route into the IP4 routing table on windows xp.
I need to have a routine/function/stored proc/whatever to archive particular accounts from our 'active'
I need to create a patching routine for my application, it's really small but
need ask you about some help. I have web app running in Net 2.0.
I need to run my development through nginx due to some complicated subdomain routing

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.