Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск Все разделы прочитаны

Опции темы Поиск в этой теме Опции просмотра
Старый 01.05.2021, 14:15   #1  
Blog bot is offline
Blog bot
24,408 / 822 (76) +++++++
Регистрация: 28.10.2006
sertandev: X++: How to get the D365FO URL correctly inside backend services

You may need to retrieve the URL of the D365 FO instance for logging or integration purposes, or for generating deep links. D365 FO foundation package provides you the URLUtility::getUrl() method for this. However this FO standard method does not work if you execute it from a batch or inside a custom API service. When we step into the code we see that it calls this method in Global class :

/// /// Gets the current request uri /// /// the current request uri internal static System.Uri GetCurrentUri() { Microsoft.Dynamics.Client.ServerForm.Contexts.SessionContext sessionContext; System.Uri currentUrl = null; sessionContext = Microsoft.Dynamics.Client.ServerForm.Contexts.SessionContext::get_Current(); if (sessionContext) { currentUrl = sessionContext.get_RequestUrl(); } return currentUrl; }Here is uses the SessionContext of ASP.NET to get the current URL. However if there is no browser session open, like in the case of services or batch runs, there is also no SessionContext and this code fails.

In that case we have other ASP.NET methods to get the URL, such as “HttpContext.Current.Request.Url.AbsoluteUri”, but the HttpContext is also not available inside X++ and I could not find any standard FO code that uses it (if you know a place that uses it, please feel free to share it in the comments). Upon further investigation, I have found this piece of code which works perfectly inside backend services as well :

using Microsoft.Dynamics.ApplicationPlatform.Environment;....................................IApplicationEnvironment env = EnvironmentFactory::GetApplicationEnvironment();str currentUrl = env.Infrastructure.HostUrl;System.Uri currentHost = new System.Uri(currentUrl);Then to get the root path of the URL you can use:

str hostUrl = currentHostUrl.GetLeftPart(System.UriPartial::Authority);To generate deeplinks using this code, put it inside a method in your own utility class and use it with the code in this blog post replacing the “URLUtility::getUrl()” call with your own method.

Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
littleax: Simple Rest API in D365FO, D365F, D365SCM Blog bot DAX Blogs 0 23.06.2020 14:12
sunilrakesh: How to get x++ methods as Microsoft Flow- Action. Example for Submit & Recall workflow. D365FO Blog bot DAX Blogs 0 04.07.2019 04:09
everythingdynamicsaxbi: How to: Successfully enable Business Events in D365FO Blog bot DAX Blogs 0 19.03.2019 23:13
NAV Team: How to Get your ERP System Up and Running in No Time Blog bot Dynamics CRM: Blogs 0 20.11.2012 23:38
dynamicsusers: How to get a good picture on someone else's developments Blog bot DAX Blogs 1 04.05.2007 09:15
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Часовой пояс GMT +3, время: 13:31.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.