构建视图页面的Servicestack错误

时间:2013-09-26 12:29:32

标签: c# razor servicestack razor-2

我使用Servicestack 3.9.59和Servicestack.Razor作为独立的控制台程序。

我的请求/响应/服务如下:

namespace Info
{
    [Route("/OverView")]
    public class OverViewRequest : IReturn<OverViewResponse>
    {}
    public class OverViewResponse
    {
        public string Name { get; set; }
    }

    public class OverViewService : Service
    {
        public OverViewResponse Get(OverViewRequest request)
        {
            return new OverViewResponse() { Name = "test" };
        }
    }
}

视图页面如下所示:

@inherits ViewPage<OverViewResponse>

@Model.Name

使用_Layout页面:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/static/css/bootstrap-theme.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
  <script src="/static/js/html5shiv.js"></script>
  <script src="/static/js/respond.min.js"></script>
<![endif]-->

</head>
<body>
    @RenderBody()
</body>
</html>

我的apphost配置如下:

namespace Info
{
class AppHost : AppHostHttpListenerBase
{
        public AppHost() : base("Server Info", typeof(AppHost).Assembly) { }

        public override void Configure(Container container)
        {
            Plugins.Add(new RazorFormat());
            Config.DefaultRedirectPath = "/OverView";
            Config.DebugMode = true;
        }
        static void Main(string[] args)
        {
            var appHost = new AppHost();
            appHost.Init();
            appHost.Start("http://*:2001/");

            Thread.Sleep(Timeout.Infinite);
        }

}
}

我尽力匹配RazorRockstars上的示例 但是,当我运行程序时,我在控制台上得到以下输出:

at ServiceStack.Razor.Managers.RazorViewManager.TrackPage(IVirtualFile file)
at ServiceStack.Common.Extensions.EnumerableExtensions.ForEach[
    T
](IEnumerable`1 values,
Action`1 action)
at ServiceStack.Razor.RazorFormat.Init()
at ServiceStack.Razor.RazorFormat.Register(IAppHost appHost)

在浏览器中访问该页面只显示ServiceStack页面生成的OverViewRequest快照。

1 个答案:

答案 0 :(得分:1)

我相信你还需要添加[DefaultView("OverView")]。这可以添加到您的服务之上或服务上的单个方法之上,例如:

[DefaultView("OverView")]
public class OverViewService : Service
{
    public OverViewResponse Get(OverViewRequest request)
    {
        return new OverViewResponse() { Name = "test" };
    }
}

或者:

public class OverViewService : Service
{
    [DefaultView("OverView")]
    public OverViewResponse Get(OverViewRequest request)
    {
        return new OverViewResponse() { Name = "test" };
    }
}

然后,您需要将OverView.cshtml文件添加到Views文件夹中。有关详细信息,请参阅此答案:ServiceStack razor default page