ListItem의 기본 디스플레이 양식으로 절대 URL을 가져옵니다
-
10-12-2019 - |
문제
ListItem의 디스플레이 양식의 절대 URL을 가져야합니다. 현재 다음과 같은 방법으로 다음과 같이합니다.
"Nofollow"> http://example.com//a> :
.
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/lists / workflowtasks / dispform.aspx? ID= 1 .
그러나이 사이트 모음이 약간 더 정교한 호스트 URL에있는 경우, 예를 들어 http : // 예제에 실패합니다..com / sites / seconcy / - 내 코드의 두 메소드는 "/ 사이트 / 초 / 파트가 포함 된 URL을 반환하여 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.