是否有可能在DNN的路由映射器中添加多个路由

时间:2015-07-29 15:18:18

标签: dotnetnuke dotnetnuke-7

我已经实施了一个模块,我将其分为两部分:

  1. 网络项目:
    将我的UI页面作为ascx和ajax调用来触发我的Web API并从中获取数据。
  2. 班级图书馆:
    哪个是我的API控制器和路由映射器类。 ALl工作正常,但浏览器处理错误为“404”。这意味着URL Not Found。我很困惑,因为我错了。
  3. 这是我的路由器映射器类: -

    using DotNetNuke.Web.Api;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI.WebControls;
    
    namespace NewController 
    {
       public class RouteMapper : IServiceRouteMapper
        {
            public void RegisterRoutes(IMapRoute mapRouteManager)
            {
                mapRouteManager.MapHttpRoute("NewController", "default", "{controller}/{action}",
                    new[] { "NewController" });
            }
        }
    }
    

    我已多次检查我的命名空间和文件夹名称,但没有发现任何错误。

    My Controller namespace is like below:-
    using DotNetNuke.Common.Utilities;
    using DotNetNuke.Web.Api;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    
    namespace NewController
    {
        public class NewInfoController : DnnApiController
        {
            [AllowAnonymous]
            [HttpGet]
            public HttpResponseMessage HelloWorld()
            {
                return Request.CreateResponse(HttpStatusCode.OK, "Hello World!");
            }
        }
    }
    

    以正确的方式获取网址以及: - 卷曲'http://localhost/dnn7/DesktopModules/NewController/API/NewInfo/HelloWorld'

    问题是因为我有多个API控制器?我需要配置多个路由吗?如果是,那么?

1 个答案:

答案 0 :(得分:5)

首先,我认为您可能对路由有误解。看起来您在网址中使用了命名空间名称。除非你的模块被称为'NewController',否则你的网址将无效。 DNN路由与Web Api路由非常相似,只是您必须指定模块文件夹名称 就这样/DesktopModules/<moduleFolderName>/API/。因此,如果您有一个名为'MyModule'的模块,并且您的控制器位于名为'NewController'的名称空间中,则地址将为

  

'http://localhost/dnn7/DesktopModules/MyModule/API/NewInfo/HelloWorld'

默认路线如下所示;

mapRouteManager.MapHttpRoute(
   "MyModule", 
   "default",
   "{controller}/{action}",
   new[] { "NewController" });

也可以用命名参数重写

mapRouteManager.MapHttpRoute(
   moduleFolderName: "MyModule",
   routeName: "default",
   url: "{controller}/{action}/{id}",
   defaults: new { id = RouteParameter.Optional },
   namespaces: new[] { "NewController" }
   );

请参阅this link for more info

关于你的第二部分,你有多条路线吗?当然;

假设您的控制器具有以下操作

namespace NewController
{
    public class NewInfoController : DnnApiController
    {
        [AllowAnonymous]
        [HttpGet]
        public HttpResponseMessage HelloWorld()
        {
            return Request.CreateResponse(HttpStatusCode.OK, "Hello World!");
        }

        [HttpGet]
        public HttpResponseMessage GetItemByName(string name)
        {
            //your logic here
        }

        [HttpGet]
        public HttpResponseMessage GetItemByNameAndLocation(string name, string location)
        {
            //your logic here
        }
    }
}

然后您的路由类看起来像这样

namespace NewController
{
    public class RouteMapper : IServiceRouteMapper
    {
        public void RegisterRoutes(IMapRoute mapRouteManager)
        {
            mapRouteManager.MapHttpRoute(
                moduleFolderName: "MyModule",
                routeName: "itemByName",
                url: "{controller}/{action}/{name}",
                defaults: new { name = "" },
                namespaces: new[] { "NewController" }
            );

            mapRouteManager.MapHttpRoute(
                moduleFolderName: "MyModule",
                routeName: "itemByNameAndLocation",
                url: "{controller}/{action}/{name}/{location}",
                defaults: new { name = "", location = "" },
                namespaces: new[] { "NewController" }
            );

            mapRouteManager.MapHttpRoute(
                moduleFolderName: "MyModule",
                routeName: "default",
                url: "{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional },
                namespaces: new[] { "NewController" }
            );
        }
    }
}

现在http://localhost/dnn7/DesktopModules/MyModule/API/NewInfo/GetItemByName/someName将由第一条路线处理,http://localhost/dnn7/DesktopModules/MyModule/API/NewInfo/GetItemByNameAndLocation/someName/someLocation将由第二条路线处理。任何与这两者不匹配的内容都将由最后default路由处理。记住路由中的层次结构非常重要!

如果您想缩短要说明的网址

,该怎么办?
  

http://localhost/dnn7/DesktopModules/MyModule/API/fetch/someName

  

http://localhost/dnn7/DesktopModules/MyModule/API/fetchByNameAndLocation/someName/someLocation

请注意,我们未指定ControllerAction。我们所要做的就是将前两条路线改为

mapRouteManager.MapHttpRoute(
    moduleFolderName: "MyModule",
    routeName: "itemByName",
    url: "fetch/{name}",
    defaults: new { controller = "NewInfo", action = "GetItemByName", name = "" },
    namespaces: new[] { "NewController" }
);

mapRouteManager.MapHttpRoute(
     moduleFolderName: "MyModule",
     routeName: "itemByNameAndLocation",
     url: "fetchByNameAndLocation/{name}/{location}",
     defaults: new { controller = "NewInfo", action = "GetItemByNameAndLocation", name = "", location = "" },
     namespaces: new[] { "NewController" }
);

如果您熟悉Web Api,您会发现DNN的服务路由非常相似,因为它只是基础Web Api路由的包装。祝你好运!