简体   繁体   English

如何使用 mvc c# 获取当前浏览器 url

[英]How to get current browser url using mvc c#

i want current browser URL (ex: http://localhost:1495/Global/General/SectionSetting) i have tried below code but i am getting API URL (ex: http://localhost:1495/api/Apiname/Functionname) i want current browser URL (ex: http://localhost:1495/Global/General/SectionSetting) i have tried below code but i am getting API URL (ex: http://localhost:1495/api/Apiname/Functionname)

string sPagePath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;

 String baseUrl = Path.GetDirectoryName(HttpContext.Current.Request.Url.OriginalString);

Thanks in advance提前致谢

In client and server disconnected architecture in asp.net Web API 2 I was used HttpContext.Header.Referrer to get the client host information. In client and server disconnected architecture in asp.net Web API 2 I was used HttpContext.Header.Referrer to get the client host information. This will just provide you the base absolute URL only.这只会为您提供基本的绝对 URL。 The browser relative path is not included on it.浏览器相对路径不包含在其中。

From you question I can understand that you need the browser address on ajax request.从你的问题我可以理解你需要 ajax 请求的浏览器地址。 One possible solution is to send the browser current url into api with ajax request.一种可能的解决方案是使用 ajax 请求将浏览器当前 url 发送到 api 中。 You can get the browser full url using javascript location .您可以使用 javascript location获取浏览器完整的 url 。 For example例如

var absolutePath = window.location;

As I don't see your code, I just try to share my thoughts.由于我没有看到您的代码,因此我只是尝试分享我的想法。 Let's see if it's helpful for you.让我们看看它是否对您有帮助。 You can share your code to get better suggestions or solutions.您可以分享您的代码以获得更好的建议或解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM