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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:57:03+00:00 2026-06-11T20:57:03+00:00

I am trying to use Url.Action() method in my js file to define urls

  • 0

I am trying to use Url.Action() method in my js file to define urls for my ajax calls. So far I have failed.

 $.ajax(
 {
    type: "POST",
    url: '@Url.Action("SomeAction", "SomeController")',
    data: { 
        fileID: rightClickedFileId
    },
    success: function (data) {

    }
 });

If i define the url in this way, browser tries to post data to

http://localhost:5907/FileManager/@Url.Action(%22SomeAction%22,%20%22SomeController%22)

and as a result my ajax call fails.

However, If I use '/SomeController/SomeAction' instead, everythings works fine.

Second works ok, but I am wondering the problem with first one ? Could it be due to routing configuration ?

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-06-11T20:57:04+00:00Added an answer on June 11, 2026 at 8:57 pm

    Url.Action is an html helper method which will work in your razor view, not in your external javascript files.

    What you can do is, get the relative url to your action method using Url.Action helper method in a razor view and set that to a javascript variable and use that in your external js file. When doing this, Always make sure to use javascript namespacing to avoid possible conflict with existing global variables.

    You may add this code in the _Layout.cshtml

    <script type="text/javascript">
    
        var yourApp = yourApp || {};
        yourApp.Urls = yourApp.Urls || {};
        yourApp.Urls.baseUrl = '@Url.Content("~")';
        yourApp.Urls.editUserUrl= '@Url.Action("Edit","User")';
    
    </script>
    

    Or in your page specific view,

    @section Scripts
    {
      <script type="text/javascript">
    
         var yourApp = yourApp || {};
         yourApp.Urls = yourApp.Urls || {};
         yourApp.Urls.baseUrl = '@Url.Content("~")';
         yourApp.Urls.editUserUrl= '@Url.Action("Edit","User")';
    
      </script>
      <script src="~/Scripts/PageSpecificExternalJsFile.js"></script>    
    }
    

    Now in your external javascript file, you can access it like

    var urlToEditUser = yourApp.Urls.editUserUrl;
    //you can use urlToEditUser  now
    
    // Or With the base url, you may safely add the remaining part of your url.
    var urlToEditUser2 = yourApp.Urls.baseUrl+"User/Edit";
    //you can use urlToEditUser2  now
    

    Always use the Url.Action or Url.RouteUrl html helper methods to build the relative url to the action methods. It will take care of correctly building the url regardless of your current page/path.

    If you want to do the same thing inside your angular controller’s/data services etc, take a look at this post which explains how to use the angular value provider to do the same.

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

Sidebar

Related Questions

I am trying to use relative url with a post ajax call as follows:
i have the following form <form action=ManageLink method=post> <input name=artistName type=text/> <input name=songName type=text/>
All, I am trying to use JQuery's URL Validator plugin. http://docs.jquery.com/Plugins/Validation/Methods/url I have a
I have been trying to use routes.rb for creating a URL /similar-to-:product (where product
I'm having a problem trying to use the UrlHelper Action() method to point to
I'm experiencing a strange problem while trying to use the Ajax.BeginForm method of ASP.NET
I'm trying to use ajax to do some validations. Its calling a method from
I am trying to use Desktop .browse() to invoke a URL, this works fine
I'm trying to use this line of code to accept URL variables with spaces
I am trying to use RestSharp's AddUrlSegment to sub the token in a URL

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.