views:

62

answers:

4

Hi , i am a newbie on web development, so i have some problems first of all what is the meaning of ~, / on an url? My real question in my javascript code i am doing some ajax calls with jquery like that

 $.ajax({
  ...
  url: '/Membership/Login',
  ...
 });

There is an Membership controller with an Login action method that i need to send data to. When i publish this project to IIS (my application is under xxx folder of wwwroot) i get wrong url address.

I get :

http://localhost/Membership/Login

I expect : (because my application is in xxx folder)

http://localhost/xxx/Membership/Login

Note : I dont want to add xxx to all urls.

A: 

When you use the form "/some/url" you are saying take me to the root of the webserver root with that url. If you want to avoid adding "xxx" to your urls you will need to either change the web server root, create a domain name and rule in IIS for that name, or use some sort of function call to generate your urls for use.

The easiest of those three is to change the webroot on the server.

Myles
+1  A: 

Using the slash makes the url relative to the root of the web server. If you want it relative to the current url, simply remove the slash character. While the tilde character has meaning in server-side controls in ASP.NET (and UNIX paths), it has no special meaning in URLs. In the context of ASP.NET, it means to make the path relative to the application, which may or may not be the web server root. In your mark up, it should be removed, either not used by you or only used in context where it is replaced by the ASP.NET framework, such as a server-side control.

tvanfosson
+2  A: 

When you use ~ in a url and call the ResolveUrl method it will put in the path to your application. You can do this in your aspx page by going:

<%=ResolveUrl("~/Membership/Login")%>

This will give you the path

/xxx/Membership/Login

which you can now give to your javascript.

Derek Ekins
I cant use <% %> syntax because js codes in .js file, is there any suggestion about that.
Yucel
Usually you have to create an object or call a method from your aspx page so use <% %> on that page and pass the url as a parameter to your js.
Derek Ekins
A: 

Thanks all of you for your answers, I cant give reputation to you sorry about that i dont have enought reputation to give vote :(.

Yucel