0

I have hosted an MVC application on my IIS under default website. So I can access the application by browsing to http://localhost/appname.

I also have a URL rewrite module which returns the same homepage if the URL entered is like this http://localhost/appname/request/*

I would like to get the path url (http://localhost/appname/)in my index.html file. I am using razor syntax. So in case I host the site under a different path or different appname, my ajax queries and css won't be affected.

How do I get the http://localhost/appname in index.html file's javascript.

1
  • RequestContext..ApplicationPath for razor , window.location.hostname for host name Commented Oct 2, 2013 at 17:41

2 Answers 2

4

You can't access the full application path (ie. www.hostname.com/appname/) in JavaScript as it has no knowledge of the server-side implementation, it's just a URL to JavaScript.

You could inject the app path into your page somewhere using @Url.Content("~/...").

<script type="text/javascript">
    var appPath = @Url.Content("~/");
</script>
<script type="text/javascript" src="@Url.Content("~/scripts/main.js")"></script>

In the above, main.js will be able to access the appPath variable.

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

2 Comments

I used that and I get appPath = http://hostname/appname/request/. Basically @Url.Content will give you whatever request server received from the client. It won't send you app path.
I will try to use RequestContext.ApplicationPath. And you are right, this is a server side implementation.
2
 var base = "@Request.Url.GetLeftPart(UriPartial.Authority)";
 var path = "@Request.ApplicationPath";
 var baseURL = base + path + "/";

Reference

Comments

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.