asp.net-mvc-3 - ServiceStack MVC自动注册路由

标签 asp.net-mvc-3 servicestack

我似乎无法让我的服务自动路由,元数据显示所有可用的类和服务,但是如果我转到/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/

相关文章:

c# - 如何检查 cshtml 中的 null/empty 值

asp.net-mvc - 有条件地在 webgrid 中显示图像 - mvc 3

Asp.net mvc3 : How to pull data from database and fill view model in one goal?

http - Servicestack 请求 header 不包含 cookie

c# - 服务栈实例之间的通信

asp.net-mvc - ASP.NET MVC - 什么是 ViewState 的等价物

asp.net - Web.config自定义错误模式冲突

c# - 检查ServiceStack记录器是否已初始化

c# - 无法以字符串格式从 JSON 重新创建 JSON 值

servicestack - 不返回迷你分析器 javascript 和 css 包含内容