Question

J'ai besoin d'une URL absolue du formulaire d'affichage d'un Listitem - actuellement, je le fais de la manière suivante:

sur http://example.com/ :

item.ParentList.ParentWeb.Url.TrimEnd('/') //"http://example.com"
+ "/" 
+ item.ParentList.DefaultDisplayFormUrl.Trim('/')//"Lists/WorkflowTasks/DispForm.aspx"
+ "?ID=" + item.ID //"?ID=1"

Ceci obtient moi l'URL correcte, ce qui signifie http://example.com/Listes / WorkflowTasks / Disform.aspx? Id= 1 .

Cependant, cela échoue lorsque la collection de site est sur une URL hôte légèrement plus sophistiquée, par exemple http:// Exemple.com / sites / secondes / - Les deux méthodes de mon code renvoient l'URL contenant le "/ Sites / secondes / partie afin que je finis avec http://example.com/sites/secondsite/sites/secondsite/lists/etc ...

Comment le code est-il d'une manière plus fiable?

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à sharepoint.stackexchange
scroll top