我正在尝试使用 wiki 页面https://github.com/ServiceStack/ServiceStack/wiki/Routing上指定的以下行在 ServiceStack 中自动注册路由
Routes.AddFromAssembly(typeof(PersonsService).Assembly);
该程序可以编译,但是当我运行它时出现错误(Persons 是我的 DTO):
Route Persons on Persons must start with a '/'
我在 ServiceStack Git 上搜索了 ServiceRoutesExtensions 类并导入到我的解决方案中。我发现它有方法
private static void AddRoute(this IServiceRoutes routes, Type requestType, string allowedVerbs)
我调试了这个方法,发现requestType参数前面没有“/”;routes.Add from metadata 的签名是
IServiceRoutes Add(Type requestType, string restPath, string verbs);
因此,restPath 参数映射到 requestType.Name:
private static void AddRoute(this IServiceRoutes routes, Type requestType, string allowedVerbs)
{
routes.Add(requestType, requestType.Name, allowedVerbs);
var hasIdField = requestType.GetProperty(IdUtils.IdField) != null;
if (!hasIdField) return;
var routePath = requestType.Name + "/{" + IdUtils.IdField + "}";
routes.Add(requestType, routePath, allowedVerbs);
}
我将方法修改为:
private static void AddRoute(this IServiceRoutes routes, Type requestType, string allowedVerbs)
{
routes.Add(requestType, "/" + requestType.Name.ToLower(), allowedVerbs);
var hasIdField = requestType.GetProperty(IdUtils.IdField) != null;
if (!hasIdField) return;
var routePath = "/" + requestType.Name.ToLower() + "/{" + IdUtils.IdField + "}";
routes.Add(requestType, routePath, allowedVerbs);
}
现在一切正常,但我不知道我是否错过了什么。