【发布时间】:2011-12-28 14:30:34
【问题描述】:
DotNetNuke 6 似乎不支持 WebMethods,因为模块被开发为用户控件,而不是 aspx 页面。
将 JSON 从 DNN 用户模块路由、调用和返回到包含该模块的页面的推荐方法是什么?
【问题讨论】:
标签: c# asp.net dotnetnuke dotnetnuke-module
DotNetNuke 6 似乎不支持 WebMethods,因为模块被开发为用户控件,而不是 aspx 页面。
将 JSON 从 DNN 用户模块路由、调用和返回到包含该模块的页面的推荐方法是什么?
【问题讨论】:
标签: c# asp.net dotnetnuke dotnetnuke-module
似乎处理此问题的最佳方法是自定义 Httphandler。我使用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/time 的调用得到正确处理并返回包含当前时间的 JSON。
【讨论】:
http://mydnnsite/svc/time 时,您知道为什么 DNN 7.3.3 会抛出 404 吗?莫非这种方式已经不支持了?
是否有其他人在通过此模块访问会话状态/更新用户信息时遇到问题?我得到了请求/响应,我可以访问 DNN 接口,但是,当我尝试获取当前用户时,它返回 null;因此无法验证访问角色。
//Always returns an element with null parameters; not giving current user
var currentUser = UserController.Instance.GetCurrentUserInfo();
【讨论】: