获取列表默认显示表单的绝对URL
-
10-12-2019 - |
题
我需要有一个listItem的显示形式的绝对网址 - 目前我这样做是以下方式:
在
.
item.ParentList.ParentWeb.Url.TrimEnd('/') //"http://example.com"
+ "/"
+ item.ParentList.DefaultDisplayFormUrl.Trim('/')//"Lists/WorkflowTasks/DispForm.aspx"
+ "?ID=" + item.ID //"?ID=1"
这让我得到了正确的URL,意思是 http://example.com/列表/ workflowtasks / disporm.aspx?ID= 1 。 但是,当我的网站集在稍微复杂的主机URL上时,这失败了,例如 http://示例.com / sites / staysite / statingite / - 来自我的代码的两种方法都返回包含“/ states / staysite / part的网址,以便我最终与 http://example.com/sites/secondsite/sites/secondsite/lists/etc ...
如何以更可靠的方式代码?
解决方案
There are a couple of methods for this available in the object model without the need to handle the slashes etc yourself, one method using MakeFullUrl:
var fullUrl = item.ParentList.ParentWeb.Site.MakeFullUrl(item.ParentList.DefaultDisplayFormUrl);
Parameters
strUrl
Type: System.String
A string that specifies the server-relative URL.
Return value Type: System.String
A string that contains the full URL.
其他提示
var parentWebUrl = item.ParentList.ParentWebUrl;
var displayFormUrl = item.ParentList.DefaultDisplayFormUrl;
var itemIdQuery = "?ID=" + item.ID;
var fullUrl = parentWeblUrl.EndWith('/') ? parentWebUrl + displayFormUrl + itemIdQuery :
parentWebUrl + "/" + displayFormUrl + itemIdQuery
Just shut down my VM and hence can't test but how about using something like SPContext.Current.Web.Url
in combination with SPList.DefaultDisplayFormUrl
. Note that SPWeb.Url
gets the absolute URL for the website.