簡體   English   中英

DNN 6模塊 - 如何利用異步調用

[英]DNN 6 Module - How to leverage asynchronous calls

DotNetNuke 6似乎不支持WebMethods,因為模塊是作為用戶控件開發的,而不是aspx頁面。

從DNN用戶模塊路由,調用和返回JSON到包含該模塊的頁面的推薦方法是什么?

處理此問題的最佳方法似乎是自定義Httphandlers。 我使用Chris Hammonds文章中的例子作為基線。

一般的想法是你需要創建一個自定義HTTP處理程序:

<system.webServer>
  <handlers>
    <add name="DnnWebServicesGetHandler" verb="*" path="svc/*" type="Your.Namespace.Handler, YourAssembly" preCondition="integratedMode" />
  </handlers>
</system.webServer>

您還需要遺留處理程序配置:

<system.web>
  <httpHandlers>
    <add verb="*" path="svc/*" type="Your.Namespace.Handler, YourAssembly" />
  </httpHandlers>
</system.web>

處理程序本身非常簡單。 您使用請求URL和參數來推斷必要的邏輯。 在這種情況下,我使用Json.Net將JSON數據返回給客戶端。

public class Handler: IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        //because we're coming into a URL that isn't being handled by DNN we need to figure out the PortalId
        SetPortalId(context.Request);
        HttpResponse response = context.Response;
        response.ContentType = "application/json";

        string localPath = context.Request.Url.LocalPath;
        if (localPath.Contains("/svc/time"))
        {
            response.Write(JsonConvert.SerializeObject(DateTime.Now));
        }

    }

    public bool IsReusable
    {
        get { return true; }
    }

    ///<summary>
    /// Set the portalid, taking the current request and locating which portal is being called based on this request.
    /// </summary>
    /// <param name="request">request</param>
    private void SetPortalId(HttpRequest request)
    {

        string domainName = DotNetNuke.Common.Globals.GetDomainName(request, true);

        string portalAlias = domainName.Substring(0, domainName.IndexOf("/svc"));
        PortalAliasInfo pai = PortalSettings.GetPortalAliasInfo(portalAlias);
        if (pai != null)
        {
            PortalId = pai.PortalID;
        }
    }

    protected int PortalId { get; set; }
}

正確處理對http:// mydnnsite / svc / time的調用並返回包含當前時間的JSON。

有沒有其他人有通過這個模塊訪問會話狀態/更新用戶信息的問題? 我得到了工作的請求/響應,我可以訪問DNN接口,但是,當我嘗試獲取當前用戶時,它返回null; 從而無法驗證訪問角色。

//Always returns an element with null parameters; not giving current user
var currentUser = UserController.Instance.GetCurrentUserInfo();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM