Reputation: 5953
On my razor view, I am wanting to put all of my jQuery into a separate JS file and then reference that file on the view with @Scripts.Render
.
On my view, I have a section jQuery that looks like this:
var settings = {};
settings.baseUri = '@Request.ApplicationPath';
var submissionUrl = "";
if (settings.baseUri === "/ProjectNameOnServer") {
submissionUrl = settings.baseUri + "/api/apiControllerName/apiControllerMethodName/";
} else {
submissionUrl = settings.baseUri + "api/apiControllerName/apiControllerMethodName/";
}
This section of code allows me to test submitting forms with api on both my localhost and live on the server without me having to change the url.
If I am testing on my localhost, then @Request.ApplicationPath == /
, but if I test on my server then @Request.ApplicationPath == /ProjectNameOnServer
.
I was wondering how I could achieve getting these same values but without the use of Razor, and instead only with jQuery.
I have tried:
var settings = {};
settings.baseUri = window.location.protocol + "//" + window.location.host;
but then settings.baseUri == http://localhost:xxxxx
. I just need exactly what @Request.ApplicationPath
is giving me.
Upvotes: 0
Views: 358
Reputation: 171690
Use location.host
to conditionally create relative path
settings.baseUri = location.host.includes('localhost') ? '/' : '/ProjectNameOnServer';
Upvotes: 1