我正在尝试使用wiki页面https://github.com/ServiceStack/ServiceStack/wiki/Routing上指定的以下行在ServiceStack中自动注册路由
Routes.AddFromAssembly(typeof(PersonsService).Assembly);
程序编译但是当我运行它时我得到错误(人是我的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的签名。从元数据添加
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);
}
现在一切正常,但我不知道是否遗漏了什么。