我似乎无法让我的服务自动路由,元数据显示所有可用的类和服务,但是如果我转到/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 样式,现在我必须手动添加,这似乎很遗憾添加每条路线。
有人有解决办法吗?
谢谢
最佳答案
是的,ServiceStack 中已经内置了路由的自动注册。使用 Routes.AddFromAssembly() 扩展方法,它将为指定程序集中的所有服务注册自定义路由(对于您已实现的所有动词),例如:
// /{RequestDto}
// /{RequestDto}/{Id} - if Id exists
Routes.AddFromAssembly(typeof(Department).Assembly);
请参阅implementation for Routes.AddFromAssembly()如果您有不同的启发式方法,请参阅如何自动注册自己的路线的模板。
关于asp.net-mvc-3 - ServiceStack MVC自动注册路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11505902/