Ottieni URL assoluto per il modulo di visualizzazione predefinito di ListItem
-
10-12-2019 - |
Domanda
Ho bisogno di avere un URL assoluto di un modulo di visualizzazione di un ListItem - attualmente lo faccio nel modo seguente:
on http://example.com/ :
item.ParentList.ParentWeb.Url.TrimEnd('/') //"http://example.com"
+ "/"
+ item.ParentList.DefaultDisplayFormUrl.Trim('/')//"Lists/WorkflowTasks/DispForm.aspx"
+ "?ID=" + item.ID //"?ID=1"
.
Questo mi prende l'URL corretto, Significato http://example.com/Elenchi / workflowtasks / dispumform.aspx? ID= 1 .
Tuttavia questo fallisce quando la raccolta del mio sito è su un URL host leggermente più sofisticato, ad esempio http:// Esempio.com / siti / secondi / - entrambi i metodi dal mio codice restituiscono l'URL contenente "/ siti / secondi / parte quindi finisco con http://example.com/sites/secondiSte/sites/secondiSte/lists/etc ...
Come lo prendi in modo più affidabile?
Soluzione
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.
Altri suggerimenti
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.