如何在不使用httpcontext.current的情况下获取域url

本文关键字:情况下 获取 url current httpcontext | 更新日期: 2024-04-23 18:11:43

我如何在不使用httpcontext.current的情况下获得域url,因为我直接使用global.asax,所以无法访问当前上下文

如何在不使用httpcontext.current的情况下获取域url

global.asax确实可以访问当前上下文,但仅在某些事件中可以访问。例如,它在Application_BeginRequest中有访问权限,但在Application_Start中没有。因此,首先,我会确保您在正确的时间尝试访问URL。

除此之外,作为global_asax所包含的HttpApplication类还为您提供了HttpRequest类型的Request属性。但是,您只能在适当的时间访问它。

好的,如果我遇到你的问题,那么你可以使用这个javascript代码来获取url。休息自己决定这个代码的时间和地点

<script type="text/javascript">
  alert('Server: ' + window.location.hostname);
  alert('Full path: ' + window.location.href);
  alert('Virtual path: ' + window.location.pathname);
  alert('HTTP path: ' + window.location.href.replace(window.location.pathname, ''));
</script>

希望对有所帮助