英文:
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>().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论