Получите абсолютный URL для формы отображения по умолчанию ListiteM
-
10-12-2019 - |
Вопрос
Мне нужно иметь абсолютный URL-адрес формы дисплея listiTem - в настоящее время я делаю это следующим образом:
на http://example.com/ :
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 / dispform.aspx? ID= 1 .
Однако это терпит неудачу, когда моя коллекция сайта находится на чуть более сложный URL-адрес хоста, например http:// пример.com / sites / secentsition / - оба метода из моего кода возвращают 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.