Obtenga URL absoluta en el formulario de visualización predeterminado de Listitem
-
10-12-2019 - |
Pregunta
Necesito tener una URL absoluta de un formulario de visualización de Listitem: actualmente lo hago de la siguiente manera:
en http://example.com/ :
item.ParentList.ParentWeb.Url.TrimEnd('/') //"http://example.com"
+ "/"
+ item.ParentList.DefaultDisplayFormUrl.Trim('/')//"Lists/WorkflowTasks/DispForm.aspx"
+ "?ID=" + item.ID //"?ID=1"
Esto me lleva la URL correcta, lo que significa http://example.com/LISTAS / WORKFLOWTASKS / DIFORM.ASPX? ID= 1 .
Sin embargo, esto falla cuando mi colección de sitios está en una URL de host ligeramente más sofisticada, por ejemplo, http:// ejemplo.com / Sitios / Sitios / SecondIRTE / - Ambos métodos de mi código devuelven la URL que contiene la "/ sitios / sitios / sitio / parte, así que termino con http://example.com/sites/secondsite/sites/secondsite/lists/etc ...
¿Cómo lo codifico de una manera más confiable?
Solución
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.
Otros consejos
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.