如何在ServiceStatck Razor页面+ ServiceStack api中导航?

时间:2013-08-04 09:05:01

标签: razor servicestack

在我的网络应用

web应用
\查看
\查看\学校
\查看\学校\ School.cshtml
\视图\学校\ Schools.cshtml

在请求和响应类中:

[Route("/v1/school", Verbs = "POST")]  
[DefaultView("School")]
public class SchoolAddRequest : School, IReturn<SchoolResponse>
{

}

public class SchoolResponse
{
    public School School { get; set; }
    public SchoolResponse()
    {
        ResponseStatus = new ResponseStatus();
        Schools = new List<School>();
    }
    public List<School> Schools { get; set; }        
    public ResponseStatus ResponseStatus { get; set; }
}

在SchoolService.cs中:

[DefaultView("School")]
public class SchoolService: Service
{       
    public SchoolResponse Post(SchoolAddRequest request)
    {
        var sch = new School {Id = "10"};
        return new SchoolResponse {School = sch, ResponseStatus = new ResponseStatus()};
    }
}

在school.cshtml中:

@inherits ViewPage<Test.Core.Services.SchoolResponse>
@{
    Layout = "_Layout";
}
<form action="/v1/School" method="POST">
   @Html.Label("Name: ")  @Html.TextBox("Name")
   @Html.Label("Address: ") @Html.TextBox("Address")
   <button type="submit">Save</button>
</form>

@if (@Model.School != null)
{
  @Html.Label("ID: ")  @Model.School.Id
}

在浏览器上:
这是假设工作,但它不是,我得到一个空白页

http://test/school/ 

这有效:

http://test/views/school/

点击'save'btn后会返回所需的响应,但浏览器上的网址为:

http://test/v1/School

我原以为是:

http://test/School 

如何让网址正常工作?不应该 http://test/School请求和回复。

1 个答案:

答案 0 :(得分:1)

http://test/school/未返回任何内容,因为您没有为该路线申请DTO和相应的“Get”服务。

您需要的是DTO请求:

[Route("/school", Verbs = "GET")]  
public class GetSchool : IReturn<SchoolResponse>
{

}

和服务......

public SchoolResponse Get(GetSchool request)
    {
        var sch = new School {Id = "10"};
        return new SchoolResponse {School = sch, ResponseStatus = new ResponseStatus()};
    }

当您点击“保存”时,系统会通过“ v1 / school ”路径向服务器发出“POST”请求,因为您指定的表单标记包含:

<form action="/v1/School" method="POST">

希望这有帮助。