简体   繁体   English

在部分视图中从 ASP.Net MVC 5 中的浏览器捕获 URL

[英]Capture URL from browser in ASP.Net MVC 5 in partial view

Ok, so I have an ASP.Net MVC 5 project.好的,所以我有一个 ASP.Net MVC 5 项目。 Each page can contain several "Widget" aka partial views.每个页面可以包含几个“小部件”,也就是部分视图。 Now I have a button in one of the partial view and when that button is clicked I want to capture the browser URL.现在我在其中一个部分视图中有一个按钮,当单击该按钮时,我想捕获浏览器 URL。 Here is my attempt: Suppose I go to https://www.elloh.com/test below is the code behind.这是我的尝试:假设我去https://www.elloh.com/test下面是后面的代码。

    Main Page
   {
      partial view one
      {
        // some razor code
      }
      partial view two
      {
            <div>@Request.Url.ToString()</div>
            <div>@Request.Url.AbsoluteUri</div>
        <button type="button" onclick="location.href='@Url.Action("Logoff", "Security")'"> LogOff</button>
      }
   }

Points: Now I some how want to pass the https://www.olleh.com/test when the LogOff button is clicked.要点:现在我想在单击LogOff按钮时如何通过https://www.olleh.com/test I have https://www.olleh.com/test in the browser tab.我在浏览器选项卡中有https://www.olleh.com/test I know I can pass the anonymous object.我知道我可以传递匿名对象。 But All I get is the URL pertaining to logout partial view.但我得到的只是与注销部分视图有关的 URL。 So the div in the second partial view displays something like https://www.olleh.com/test/partialView2 .因此,第二个局部视图中的div显示类似https://www.olleh.com/test/partialView2的内容。 So if I do something like below it will not work.因此,如果我执行以下操作,它将无法正常工作。

    <button type="button" onclick="location.href='@Url.Action("Logoff", 
"Security",  new {id = @Request.Url.ToString()})'"> LogOff</button>

PS: the code snippet is not correct syntax wise. PS:代码片段的语法不正确。 Bear with me on this.请容忍我。

Expected output: Need to be able to capture Browser URL when button in partial view is clicked.预期输出:点击局部视图中的按钮时需要能够捕获浏览器 URL。

HttpContext.Current.Request.Url Or use Raw URL HttpContext.Current.Request.Url 或使用原始 URL

https://msdn.microsoft.com/en-us/library/system.web.httprequest.rawurl.aspx https://msdn.microsoft.com/en-us/library/system.web.httprequest.rawurl.aspx

您可以使用HttpContext.Current.Request.UrlReferrer属性来完成。

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

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