1

我为我的服务设置了两条路线:

GET /foo
GET /foo/{Name}

元数据页面正确列出:

GET /foo
GET /foo/{Name}

但是当我浏览到/baseurl/foo/NameValueHere我得到

The operation 'NameValueHere' does not exist for this service

我的(自托管)应用程序主机中是否缺少某些配置?

编辑:

附加信息

我不喜欢 DTO 上的 Route 属性,所以我为IServiceRoutes.

public static IServiceRoutes AddFromServiceAttributes(this IServiceRoutes routes)
{
    var myServiceTypes = typeof(MyServiceBase).Assembly.GetDerivedTypesOf<MyServiceBase>();
    var routedMethods = myServiceTypes.SelectMany(type => type.GetMethodsWithAttribute<MyServiceRouteAttribute>());

    foreach (var routedMethod in routedMethods)
    {
        var routesFound = routedMethod.GetAttributes<MyServiceRouteAttribute>();

        foreach (var route in routesFound)
        {
            // request type can be inferred from the method's first param
            // and the same for allowed verbs from the method's name
            // [MyServiceRoute(typeof(ReqType), "/foo/{Name}", "GET")]
            // [MyServiceRoute("/foo/{Name}", "GET")]
            // [MyServiceRoute(typeof(ReqType), "/foo/{Name}")]

            if (route.RequestType == null)
            {
                route.RequestType = routedMethod.GetParameterListFromCache().First().ParameterType;
            }

            if (string.IsNullOrWhiteSpace(route.Verbs))
            {
                var upperRoutedMethodName = routedMethod.Name.ToUpperInvariant();
                route.Verbs = upperRoutedMethodName != "ANY" ? upperRoutedMethodName : null;
            }

            routes.Add(route.RequestType, route.RestPath, route.Verbs);
        }

    }

    return routes;
}

我将此方法称为AppHost.ConfigureAddFromAssembly

this.SetConfig(new EndpointHostConfig { ServiceStackHandlerFactoryPath = "service" });
// some container registrations here
this.Routes.AddFromServiceAttributes().AddFromAssembly();

令我困惑的是元数据页面正确显示了路线。

DTO 非常简单,它们确实包含Name字符串属性。

class Foo { public string Name { get; set; } }

编辑 2:我删除了MyServiceRouteAttribute属性并重用了 ServiceStack 的RouteAttribute. 请求 DTO 类型是从第一个方法参数推断出来的。

编辑3:可能我设法解决了这个问题。我/json/reply在网址中预先设置。

http://localhost/service/json/reply/foo/NameValueHere <- not working
http://localhost/service/foo/NameValueHere <- working

我认为内容类型和回复类型标记都是强制性的。

4

0 回答 0