1

I have a localhost with a few projects, so this project in VS2010 properties->web->use local IIS web server = 'localhost/project4/'. (I can't use the normal debug VS Development Server as some components won't work in my project) Anyhow, localhost is fine no big deal, so I go on coding.

Now, in ajax when I call .load('/Account/LogOn'); it gives me 'localhost/Account/LogOn' but what I really want is 'localhost/project4/Account/LogOn' because it is sitting in the project4 directory, not the root.

Any idea how to tell ajax I need that virtual directory prefix between domain name and the rest of the url?

EDIT --------------------------------------------

Thanks guys, combined with all your knowledge, I guess the best ways to do it are:

  1. Include the js script into .cshtml server side and use "~/Account/LogOn/" let .net figure out the path.

  2. Make a global var remove_me_debug_only_virtual_directory = "/project4/"; in js code. append it to the domain name. this way we don't have to pull .net into the water and write js code in .cshtml.

  3. Move the project to localhost/ root if you can, in this case I can't, because other people at work wants to access this networked server and do demo.

3
  • 1
    Have you tried .load('Account/LogOn'); (without the preceding /)? Commented Jun 28, 2012 at 11:58
  • @J Steen, I think tilda only works on server side like @Url.Content("~/Account/LogOn") in a .cshtml or .aspx? Doesn't seem to work in js file? I will end up having 'localhost/~/Account/LogOn' and .load() will not parse it for me? Commented Jun 28, 2012 at 12:06
  • @Anthony G, without preceding "/" would give more problem, say, u have an ajax LogOnPartial first time calling from Home/Index/ u will get 'localhost/project4/Account/logOn', good. but when you go to Account/Index/ call that LogOnPartial again, u will get 'localhost/project4/Account/Account/LogOn', repeated twice, which is no good. Commented Jun 28, 2012 at 12:11

3 Answers 3

3

If your JS code is in an MVC cshtml file, use it like this:

.load('@Url.Action("LogOn", "Account")');

The proper URL will be placed in the concrete JS code.

Sign up to request clarification or add additional context in comments.

Comments

2

According to .net documents,

Absolute and relative path references in a server control have the following disadvantages:

Absolute paths are not portable between applications. If you move the application that the absolute path points to, the links will break.

Relative paths in the style of client elements can be difficult to maintain if you move resources or pages to different folders.

To overcome these disadvantages, ASP.NET includes the Web application root operator (~), which you can use when specifying a path in server controls. ASP.NET resolves the ~ operator to the root of the current application. You can use the ~ operator in conjunction with folders to specify a path that is based on the current root.

So as described above, using ~ will lead you to the root of your web project using asp:

 <asp:image runat="server" id="Image1" ImageUrl="~/Images/SampleImage.jpg" />

For more information go to: Web Project Paths

Based on your update: Possible duplicate of: base url using jQuery

Where stated by @gregguida:

  var l = window.location;
  var base_url = l.protocol + "//" + l.host + "/" + l.pathname.split('/')[1];

  //all you need to do is work with l.pathname.split('/') to find what you need.

3 Comments

hmmm, as I mentioned above, Razor @Url.Content("~/Account/LogOn") or WebForm <asp:image id="img1" Url="~/Account/LogOn" runat="SERVER">, these are server side code. I am talking about js code ajax .load()...
Are you saying .load() does not execute your asp code before loading? because load() read content from server then returns the corresponding html.
sorry, my bad, I modified the title a bit now. I mean the .load() function in js JQuery one. Not the server side one.
1

I am a java developer but the context is same in both case, In your can follow the steps

  1. Check the context of your server if root is "/" then you have to call something like,

    .load('/project4/Account/LogOn');

  2. If your application is root and you can calling this request from itself then

    .load('Account/LogOn');

2 Comments

I thought about hard coding '/project4/' into the script, but isn't it a bit wrecky and hacky when I try deploy the project to a real domain or re-use the code in the future?
I java we use to use request.getConextPath for resolving such conflicts. You should have something like that in .net also

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.