ServiceStack MVC自动注册路由

时间:2012-07-16 13:57:38

标签: asp.net-mvc-3 servicestack

我似乎无法让我的服务自动路由,MetaData显示所有可用的类和服务,但是我应该去/ api / btbCustomerEvents我得到未处理的路由错误。

我试过这个:

[Alias("btbCustomerEvents")]
[RestService("/btbCustomerEvents")]
public class Btbcustomerevent : BaseModel

我的AppHost看起来像这样:

public class AppHost: AppHostBase
{       
    public AppHost() : base("Energy System API", typeof(DepartmentService).Assembly) { }

    public override void Configure(Funq.Container container)
    {
        //Set JSON web services to return idiomatic JSON camelCase properties
        ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;

        //register all routes
        Routes
            .Add<Department>("/Departments")
            .Add<Department>("/Departments/{Id}")
            .Add<Paymentmethod>("/PaymentMethods")
            .Add<Paymentmethod>("/PaymentMethods/{Id}")
            .Add<MyExampleModel>("/MyExampleModel")
            .Add<MyExampleModel>("/MyExampleModel/{Id}");

        //Change the default ServiceStack configuration
        SetConfig(new EndpointHostConfig{ DebugMode = true, });

        container.Register<ICacheClient>(new MemoryCacheClient());
        container.Register<ISessionFactory>(c => 
            new SessionFactory(c.Resolve<ICacheClient>()));

        ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
    }

    public static void Start()
    {
        new AppHost().Init();
    }

我真的不想添加所有内容的路径,我创建了TT文件,从数据库创建模型,并自动添加其余服务/ CRUD样式,现在我只需要手动添加每条路线和每条路线。

任何人都有解决方案吗?

感谢

1 个答案:

答案 0 :(得分:2)

是的,路由的自动注册已经内置到ServiceStack中。使用Routes.AddFromAssembly()扩展方法,它将为指定的程序集中的所有服务注册自定义路由(对于您实现的所有动词),例如:

//    /{RequestDto}
//    /{RequestDto}/{Id} - if Id exists
Routes.AddFromAssembly(typeof(Department).Assembly);            

如果您有不同的启发式方法,请参阅implementation for Routes.AddFromAssembly()以获取如何自动注册您自己的路线的模板。