How to get the page origin in razor?
I'm looking for the equivalent of this javascript
window.location.origin
but server side, while building mvc pages.
For example, if you are here
http://website.com/123,
it would return
http://website.com
Its important that i have the "http://" part
asp.net-mvc asp.net-mvc-3 razor request
add a comment |
I'm looking for the equivalent of this javascript
window.location.origin
but server side, while building mvc pages.
For example, if you are here
http://website.com/123,
it would return
http://website.com
Its important that i have the "http://" part
asp.net-mvc asp.net-mvc-3 razor request
Turned out that was already asked... possible duplicate of How to get current page URL in MVC 3
– Alexei Levenkov
Aug 6 '12 at 16:07
add a comment |
I'm looking for the equivalent of this javascript
window.location.origin
but server side, while building mvc pages.
For example, if you are here
http://website.com/123,
it would return
http://website.com
Its important that i have the "http://" part
asp.net-mvc asp.net-mvc-3 razor request
I'm looking for the equivalent of this javascript
window.location.origin
but server side, while building mvc pages.
For example, if you are here
http://website.com/123,
it would return
http://website.com
Its important that i have the "http://" part
asp.net-mvc asp.net-mvc-3 razor request
asp.net-mvc asp.net-mvc-3 razor request
edited Jul 21 '14 at 13:02
Tshepang
6,0651772112
6,0651772112
asked Aug 6 '12 at 16:01
odle
2,05471931
2,05471931
Turned out that was already asked... possible duplicate of How to get current page URL in MVC 3
– Alexei Levenkov
Aug 6 '12 at 16:07
add a comment |
Turned out that was already asked... possible duplicate of How to get current page URL in MVC 3
– Alexei Levenkov
Aug 6 '12 at 16:07
Turned out that was already asked... possible duplicate of How to get current page URL in MVC 3
– Alexei Levenkov
Aug 6 '12 at 16:07
Turned out that was already asked... possible duplicate of How to get current page URL in MVC 3
– Alexei Levenkov
Aug 6 '12 at 16:07
add a comment |
4 Answers
4
active
oldest
votes
I'm a fan of
string url = Request.Url.PathAndQuery.length > 1
? Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, string.Empty)
: url;
Keeps your Http/Https, Port (if applicable), and HostName/IP.
DotNetFiddle Examples
Updated to Account for PathAndQuery length of 1.
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
That does it, kind sir
– odle
Aug 6 '12 at 16:32
1
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
1
This will not work ifRequest.Url.PathAndQuery
is equal to/
i.e empty.
– Ibraheem
Nov 20 '18 at 12:32
|
show 1 more comment
you could try
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority)
Or
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Host)
Authority will include the port number
I think you'll have an extra:
on your Authority.
– Erik Philips
Aug 6 '12 at 16:12
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
add a comment |
I think you looking for Request.Url or RawUrl.
Uri.Scheme of Request.Url will give you info on http/https difference.
Request.Url will also include/123
.
– John H
Aug 6 '12 at 16:07
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
Neither of these will give what was requested. Taking the following URL into considerationhttp://localhost:63105/home/index
, Request.Url will returnhttp://localhost:63105/home/index
and Request.RawUrl will return/home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.
– John H
Aug 6 '12 at 16:20
add a comment |
The window.location.origin in javascript returns the protocol, port (if any), domain and extension of the current url.
If you want to get the same information from an URL, the accepted answers will provide that to you.
If you want the same behavior, ie a piece of javascript is calling your server method and you want to know where it is calling from, you can inspect the HttpRequest.URLReferrer. However this can be spoofed easily and thus is not reliable.
UsingUrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.
– RobIII
Sep 12 '16 at 14:18
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11831850%2fhow-to-get-the-page-origin-in-razor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm a fan of
string url = Request.Url.PathAndQuery.length > 1
? Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, string.Empty)
: url;
Keeps your Http/Https, Port (if applicable), and HostName/IP.
DotNetFiddle Examples
Updated to Account for PathAndQuery length of 1.
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
That does it, kind sir
– odle
Aug 6 '12 at 16:32
1
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
1
This will not work ifRequest.Url.PathAndQuery
is equal to/
i.e empty.
– Ibraheem
Nov 20 '18 at 12:32
|
show 1 more comment
I'm a fan of
string url = Request.Url.PathAndQuery.length > 1
? Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, string.Empty)
: url;
Keeps your Http/Https, Port (if applicable), and HostName/IP.
DotNetFiddle Examples
Updated to Account for PathAndQuery length of 1.
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
That does it, kind sir
– odle
Aug 6 '12 at 16:32
1
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
1
This will not work ifRequest.Url.PathAndQuery
is equal to/
i.e empty.
– Ibraheem
Nov 20 '18 at 12:32
|
show 1 more comment
I'm a fan of
string url = Request.Url.PathAndQuery.length > 1
? Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, string.Empty)
: url;
Keeps your Http/Https, Port (if applicable), and HostName/IP.
DotNetFiddle Examples
Updated to Account for PathAndQuery length of 1.
I'm a fan of
string url = Request.Url.PathAndQuery.length > 1
? Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, string.Empty)
: url;
Keeps your Http/Https, Port (if applicable), and HostName/IP.
DotNetFiddle Examples
Updated to Account for PathAndQuery length of 1.
edited Nov 20 '18 at 20:47
answered Aug 6 '12 at 16:11
Erik Philips
40.1k689123
40.1k689123
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
That does it, kind sir
– odle
Aug 6 '12 at 16:32
1
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
1
This will not work ifRequest.Url.PathAndQuery
is equal to/
i.e empty.
– Ibraheem
Nov 20 '18 at 12:32
|
show 1 more comment
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
That does it, kind sir
– odle
Aug 6 '12 at 16:32
1
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
1
This will not work ifRequest.Url.PathAndQuery
is equal to/
i.e empty.
– Ibraheem
Nov 20 '18 at 12:32
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: This is the best answer.
– John H
Aug 6 '12 at 16:13
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
+1: Clean and readable
– Manatherin
Aug 6 '12 at 16:15
That does it, kind sir
– odle
Aug 6 '12 at 16:32
That does it, kind sir
– odle
Aug 6 '12 at 16:32
1
1
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
+1. Obviously this is not how one should manipulate Url (UriBuilder would be safer) but ok for most cases.
– Alexei Levenkov
Aug 6 '12 at 16:37
1
1
This will not work if
Request.Url.PathAndQuery
is equal to /
i.e empty.– Ibraheem
Nov 20 '18 at 12:32
This will not work if
Request.Url.PathAndQuery
is equal to /
i.e empty.– Ibraheem
Nov 20 '18 at 12:32
|
show 1 more comment
you could try
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority)
Or
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Host)
Authority will include the port number
I think you'll have an extra:
on your Authority.
– Erik Philips
Aug 6 '12 at 16:12
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
add a comment |
you could try
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority)
Or
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Host)
Authority will include the port number
I think you'll have an extra:
on your Authority.
– Erik Philips
Aug 6 '12 at 16:12
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
add a comment |
you could try
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority)
Or
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Host)
Authority will include the port number
you could try
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Authority)
Or
@String.Format("{0}://{1}", Request.Url.Scheme, Request.Url.Host)
Authority will include the port number
answered Aug 6 '12 at 16:08
Manatherin
3,50143148
3,50143148
I think you'll have an extra:
on your Authority.
– Erik Philips
Aug 6 '12 at 16:12
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
add a comment |
I think you'll have an extra:
on your Authority.
– Erik Philips
Aug 6 '12 at 16:12
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
I think you'll have an extra
:
on your Authority.– Erik Philips
Aug 6 '12 at 16:12
I think you'll have an extra
:
on your Authority.– Erik Philips
Aug 6 '12 at 16:12
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
@ErikPhilips It doesn't seem to in my test
– Manatherin
Aug 6 '12 at 16:15
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
oh yeah you're right, forgot were the port is on url, I use it so rarely.
– Erik Philips
Aug 6 '12 at 16:18
add a comment |
I think you looking for Request.Url or RawUrl.
Uri.Scheme of Request.Url will give you info on http/https difference.
Request.Url will also include/123
.
– John H
Aug 6 '12 at 16:07
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
Neither of these will give what was requested. Taking the following URL into considerationhttp://localhost:63105/home/index
, Request.Url will returnhttp://localhost:63105/home/index
and Request.RawUrl will return/home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.
– John H
Aug 6 '12 at 16:20
add a comment |
I think you looking for Request.Url or RawUrl.
Uri.Scheme of Request.Url will give you info on http/https difference.
Request.Url will also include/123
.
– John H
Aug 6 '12 at 16:07
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
Neither of these will give what was requested. Taking the following URL into considerationhttp://localhost:63105/home/index
, Request.Url will returnhttp://localhost:63105/home/index
and Request.RawUrl will return/home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.
– John H
Aug 6 '12 at 16:20
add a comment |
I think you looking for Request.Url or RawUrl.
Uri.Scheme of Request.Url will give you info on http/https difference.
I think you looking for Request.Url or RawUrl.
Uri.Scheme of Request.Url will give you info on http/https difference.
answered Aug 6 '12 at 16:05
Alexei Levenkov
83.9k890132
83.9k890132
Request.Url will also include/123
.
– John H
Aug 6 '12 at 16:07
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
Neither of these will give what was requested. Taking the following URL into considerationhttp://localhost:63105/home/index
, Request.Url will returnhttp://localhost:63105/home/index
and Request.RawUrl will return/home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.
– John H
Aug 6 '12 at 16:20
add a comment |
Request.Url will also include/123
.
– John H
Aug 6 '12 at 16:07
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
Neither of these will give what was requested. Taking the following URL into considerationhttp://localhost:63105/home/index
, Request.Url will returnhttp://localhost:63105/home/index
and Request.RawUrl will return/home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.
– John H
Aug 6 '12 at 16:20
Request.Url will also include
/123
.– John H
Aug 6 '12 at 16:07
Request.Url will also include
/123
.– John H
Aug 6 '12 at 16:07
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
@JohnH, I've updated the answer... Also Manatherin's answer(+1) already includes all.
– Alexei Levenkov
Aug 6 '12 at 16:14
Neither of these will give what was requested. Taking the following URL into consideration
http://localhost:63105/home/index
, Request.Url will return http://localhost:63105/home/index
and Request.RawUrl will return /home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.– John H
Aug 6 '12 at 16:20
Neither of these will give what was requested. Taking the following URL into consideration
http://localhost:63105/home/index
, Request.Url will return http://localhost:63105/home/index
and Request.RawUrl will return /home/index
. Both of our answers are inferior to Erik's, so I'll remove mine.– John H
Aug 6 '12 at 16:20
add a comment |
The window.location.origin in javascript returns the protocol, port (if any), domain and extension of the current url.
If you want to get the same information from an URL, the accepted answers will provide that to you.
If you want the same behavior, ie a piece of javascript is calling your server method and you want to know where it is calling from, you can inspect the HttpRequest.URLReferrer. However this can be spoofed easily and thus is not reliable.
UsingUrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.
– RobIII
Sep 12 '16 at 14:18
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
add a comment |
The window.location.origin in javascript returns the protocol, port (if any), domain and extension of the current url.
If you want to get the same information from an URL, the accepted answers will provide that to you.
If you want the same behavior, ie a piece of javascript is calling your server method and you want to know where it is calling from, you can inspect the HttpRequest.URLReferrer. However this can be spoofed easily and thus is not reliable.
UsingUrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.
– RobIII
Sep 12 '16 at 14:18
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
add a comment |
The window.location.origin in javascript returns the protocol, port (if any), domain and extension of the current url.
If you want to get the same information from an URL, the accepted answers will provide that to you.
If you want the same behavior, ie a piece of javascript is calling your server method and you want to know where it is calling from, you can inspect the HttpRequest.URLReferrer. However this can be spoofed easily and thus is not reliable.
The window.location.origin in javascript returns the protocol, port (if any), domain and extension of the current url.
If you want to get the same information from an URL, the accepted answers will provide that to you.
If you want the same behavior, ie a piece of javascript is calling your server method and you want to know where it is calling from, you can inspect the HttpRequest.URLReferrer. However this can be spoofed easily and thus is not reliable.
edited Sep 13 '16 at 18:23
answered Dec 12 '14 at 23:42
Jan Van der Haegen
270210
270210
UsingUrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.
– RobIII
Sep 12 '16 at 14:18
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
add a comment |
UsingUrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.
– RobIII
Sep 12 '16 at 14:18
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
Using
UrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.– RobIII
Sep 12 '16 at 14:18
Using
UrlReferrer
is dangerous! It can be spoofed / set to anything. That opens a whole big can of worms if you're not escaping correctly etc. (XSS for example). Also, when I hit the page coming from, say, Google.com your method will return google.com, not website.com. Never trust user-input; the referrer is exactly that.– RobIII
Sep 12 '16 at 14:18
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
@RobIII You're correct. Updated my answer to reflect my original opinion.
– Jan Van der Haegen
Sep 13 '16 at 18:23
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f11831850%2fhow-to-get-the-page-origin-in-razor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Turned out that was already asked... possible duplicate of How to get current page URL in MVC 3
– Alexei Levenkov
Aug 6 '12 at 16:07