Dotnetnuke 是否有可能在DNN';s路线制图器

Dotnetnuke 是否有可能在DNN';s路线制图器,dotnetnuke,dotnetnuke-7,Dotnetnuke,Dotnetnuke 7,我已经实现了一个模块,其中我将其分为两部分: Web项目: 它将我的UI页面作为ascx和ajax调用来触发我的Web API并从中获取数据 类库: 它有我的API控制器和路由映射器类。所有工作正常,但浏览器处理了一个错误为“404”。这意味着找不到URL。我很困惑,我错在哪里了 这是我的路由器映射器类:- using DotNetNuke.Web.Api; using System; using System.Collections.Generic; using System.Linq; us

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

  • Web项目:
    它将我的UI页面作为ascx和ajax调用来触发我的Web API并从中获取数据
  • 类库:
    它有我的API控制器和路由映射器类。所有工作正常,但浏览器处理了一个错误为“404”。这意味着找不到URL。我很困惑,我错在哪里了
  • 这是我的路由器映射器类:-

    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!");
            }
        }
    }
    
    以正确的方式获取URL,以及:- 卷曲“


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

    首先,我想您可能对路线有误解。看起来您正在url中使用名称空间名称。除非您的模块名为“NewController”,否则您的url将无法工作。DNN路由与Web Api路由非常相似,只是您必须指定模块文件夹名称 因此
    /DesktopModules//API/
    。因此,如果您有一个名为
    'MyModule'
    的模块,并且您的控制器位于名为
    'NewController'
    的命名空间中,那么地址将是

    ''

    默认路由将如下所示

    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" }
       );
    

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

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

    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
    路径处理。请记住,路由中的层次结构很重要

    如果你想缩短URL的长度,怎么办

    请注意,我们没有指定
    控制器
    操作
    。我们所要做的就是将前两条路线改为

    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路由的包装器。祝你好运

    我正在努力理解你的问题。当您尝试使用带有的浏览器测试服务时,是否出现404错误?如果是这样,您的类库DLL是否已内置到DNN的/bin文件夹中?