我已经实施了一个模块,我将其分为两部分:
这是我的路由器映射器类: -
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控制器?我需要配置多个路由吗?如果是,那么?
答案 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" }
);
关于你的第二部分,你有多条路线吗?当然;
假设您的控制器具有以下操作
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
请注意,我们未指定Controller
和Action
。我们所要做的就是将前两条路线改为
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路由的包装。祝你好运!