DotNetNuke 6 似乎不支持 WebMethods,因为模块被开发为用户控件,而不是 aspx 页面。
将 JSON 从 DNN 用户模块路由、调用和返回到包含该模块的页面的推荐方法是什么?
DotNetNuke 6 似乎不支持 WebMethods,因为模块被开发为用户控件,而不是 aspx 页面。
将 JSON 从 DNN 用户模块路由、调用和返回到包含该模块的页面的推荐方法是什么?
处理这个问题的最好方法似乎是自定义 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();