如何从Kentico 13核心的.cshtml文件中获取当前页面URL?

huangapple go评论42阅读模式
英文:

How to get current page URL in .cshtml from Kentico 13 core?

问题

如何在Kentico 13的.cshtml页面中获取当前页面URL。

使用**@Context.Request.GetDisplayUrl().ToString()**,但出现以下错误
'HttpRequest'不包含'GetDisplayUrl'的定义,且找不到接受类型为'HttpRequest'的第一个参数的可访问扩展方法'GetDisplayUrl'的定义(是否缺少使用指令或程序集引用?)

英文:

How could we get the current page URL in .cshtml page for Kentico 13.

Used @Context.Request.GetDisplayUrl().ToString()
but getting the below error
'HttpRequest' does not contain a definition for 'GetDisplayUrl' and no accessible extension method 'GetDisplayUrl' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?)

答案1

得分: 1

使用这个命名空间: "@using Microsoft.AspNetCore.Http.Extensions"。查看图像以获得更好的体验

英文:

Use this namespace "@using Microsoft.AspNetCore.Http.Extensions"
see the image for better experiance

答案2

得分: 1

我强烈推荐查看“Dancing Goat”示例,您可以安装它(在您的本地机器上使用Xperience安装管理器),以获得一些灵感。要具体回答您的问题,您可以使用这个来实现您的截图显示的规范URL:

<link rel="canonical" href="@Url.Kentico().PageCanonicalUrl()" />

英文:

I highly recommend checking out the Dancing Goat example which you can install (on your local machine using Xperience Installation Manager) to get some insipration from. To answer your question specifically, you can use this to achieve what your screenshot is showing for the canonical URL:

<link rel="canonical" href="@Url.Kentico().PageCanonicalUrl()" />

答案3

得分: 0

我通常使用IPageUrlRetriever进行此操作。

这需要当前文档,您可以使用IPageDataContextRetriever获取。

我会在您的控制器中调用两者,并将结果放入您的视图模型,但您也可以只在cshtml文件中调用CMS.Core.Service.Resolve<SERVICETYPE>()来调用这两个。

英文:

I usually do this with the IPageUrlRetriever.

This needs the current document, which you could get with the IPageDataContextRetriever.

I would invoke both in your controller and put the result into your viewmodel, but you can also invoke both in yout cshtml file only by using CMS.Core.Servive.Resolve<SERVICETYPE>().

https://docs.xperience.io/developing-websites/retrieving-content/displaying-page-content#Displayingpagecontent-GettingpageURLs

huangapple
  • 本文由 发表于 2023年4月6日 21:15:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定