2013-08-04 31 views
1

在我的webapp如何在ServiceStatck Razor頁面+ ServiceStack api中導航?

WEBAPP
\查看
\查看\學校
\視圖\學校\ School.cshtml
\查看\學校\ Schools.cshtml

在Request和Response類:

[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/ 

在碰到「保存」 BTN返回所需的響應,但在瀏覽器的網址是:

http://test/v1/School 

我期待它是:

http://test/School 

我怎樣才能獲得url工作對。?根據要求和迴應,它不應該是 http://test/School

回答

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()}; 
    } 

當你點擊「保存」,一個「POST」請求將服務器進行通過路由 'V1 /學校',因爲你指定的表單標籤有:

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

希望這有助於。