How to get the page origin in razor?












8














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










share|improve this question
























  • 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


















8














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










share|improve this question
























  • 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
















8












8








8


1





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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














4 Answers
4






active

oldest

votes


















13














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.






share|improve this answer























  • +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 if Request.Url.PathAndQuery is equal to / i.e empty.
    – Ibraheem
    Nov 20 '18 at 12:32



















1














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






share|improve this answer





















  • 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



















0














I think you looking for Request.Url or RawUrl.



Uri.Scheme of Request.Url will give you info on http/https difference.






share|improve this answer





















  • 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 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



















-1














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.






share|improve this answer























  • 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











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
});


}
});














draft saved

draft discarded


















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









13














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.






share|improve this answer























  • +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 if Request.Url.PathAndQuery is equal to / i.e empty.
    – Ibraheem
    Nov 20 '18 at 12:32
















13














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.






share|improve this answer























  • +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 if Request.Url.PathAndQuery is equal to / i.e empty.
    – Ibraheem
    Nov 20 '18 at 12:32














13












13








13






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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 if Request.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: 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 if Request.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













1














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






share|improve this answer





















  • 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
















1














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






share|improve this answer





















  • 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














1












1








1






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






share|improve this answer












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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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











0














I think you looking for Request.Url or RawUrl.



Uri.Scheme of Request.Url will give you info on http/https difference.






share|improve this answer





















  • 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 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
















0














I think you looking for Request.Url or RawUrl.



Uri.Scheme of Request.Url will give you info on http/https difference.






share|improve this answer





















  • 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 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














0












0








0






I think you looking for Request.Url or RawUrl.



Uri.Scheme of Request.Url will give you info on http/https difference.






share|improve this answer












I think you looking for Request.Url or RawUrl.



Uri.Scheme of Request.Url will give you info on http/https difference.







share|improve this answer












share|improve this answer



share|improve this answer










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 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


















  • 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 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
















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











-1














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.






share|improve this answer























  • 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
















-1














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.






share|improve this answer























  • 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














-1












-1








-1






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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Sep 13 '16 at 18:23

























answered Dec 12 '14 at 23:42









Jan Van der Haegen

270210




270210












  • 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


















  • 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
















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


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin