2011-12-28 40 views
3

DotNetNuke 6似乎不支持WebMethods,因为模块是作为用户控件开发的,而不是aspx页面。DNN 6模块 - 如何利用异步调用

将DNN用户模块的JSON路由,调用和返回到包含该模块的页面的推荐方式是什么?

回答

3

它似乎是处理这个问题的最佳方法是自定义Httphandlers。我使用Chris Hammonds Article中找到的示例作为基线。

总的想法是,你需要创建一个自定义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 /时间被正确地处理,并返回包含当前时间JSON。

+5

我得到StackOverflow点吗? ;) – 2012-01-02 06:40:10

+0

@克里斯哈蒙德,你会得到一个漂亮的谢谢,因为你有足够的分数。 – tatigo 2014-05-15 02:32:06

+0

heh当时我没有那么多:D – 2014-05-15 02:34:19

0

没有其他人有通过这个模块访问会话状态/更新用户信息的问题?我得到了请求/响应的工作,并且我可以访问DNN接口,但是,当我尝试获取当前用户时,它将返回null;从而无法验证访问角色。

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