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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:18:29+00:00 2026-05-15T15:18:29+00:00

I am doing an ajax Post action and i need to work out the

  • 0

I am doing an ajax “Post” action and i need to work out the URL of my action.

To do this i am using

   var url = '<%= Url.Action("Action", "Controller") %>';

But this code lives in a rather large Js file that is loading into my MVC View. I dont want to move it all into my view as it is too big and will look a mess.

I tried setting my JS include to runat=”server” but this errors.

<script runat="server" src="<%: ResolveUrl("~/Scripts/Custom/MyScript.js")%>" type="text/javascript" ></script>

Can this be done?

Thanks,
Kohan

  • 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-15T15:18:30+00:00Added an answer on May 15, 2026 at 3:18 pm

    No you can’t put ASP.NET in a javascript file. Javascript files are static resources which should be directly served by your web server. They should never contain any server side language. You could define url as a global variable in the view:

    <script type="text/javascript">
        var url = '<%= Url.Action("Action", "Controller") %>';
    </script>
    

    then use this url variable in your javascript file.

    Notice tough that depending on what you are doing with this variable there might be much better ways. For example let’s suppose that in your view you have an anchor that you want to ajaxify:

    <%: Html.ActionLink("some text", "actionName", new { id = "fooAnchor" }) %>
    

    Then in your javascript you could:

    $(function() {
        $('#fooAnchor').click(function() {
            $('#result').load(this.href);
            return false;
        });
    });
    

    So no need of declaring any variables at all. Another option is to define a javascript function which takes parameters so that you can pass it whatever you need.

    Conclusion: think of your javascript files as reusable components that tomorrow you might plug into your cool site based on Java Servlets for example and host them on a CDN. You could even try to convert them to reusable jQuery plugins. Don’t try to mix it with server side languages as it leads to a strong coupling, ugly code – tag soup.

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

Sidebar

Related Questions

I'm doing a jQuery AJAX post like this: var form = $(#formid); form.submit(function() {
I have this code for doing an ajax request to a webservice: var MyCode
I'm having trouble updating the DOM when doing an AJAX post using JQuery. This
I'm doing the following: // Post to the server $.ajax({ type: 'POST', url: $(form.edit_item).attr(action),
After doing so: $.ajax({ type: POST, url: $(#form).attr(action), data: value= + value + &sel=
I am doing a simple $.ajax request: $.ajax({ type: POST, url: /Run/ + param1
Doing an ajax get request works as expected using the following code: $.ajax({ type:
I have the following ajax serialize: $.ajax({ type: POST, url: $form.attr('action'), data: $form.serialize(), error:
In my html I have <h2>Title goes here</h2> @using (Html.BeginForm(Action, Controller, FormMethod.Post, new {
I need to come up with a smarter/simpler way of doing ajax calls in

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.