2013-03-21 95 views
1

我做在ASP.NET 4.5 Web窗體FriendlyUrls一個簡單的測試,Foo.aspx變爲/美孚/(它的工作原理)。 當我嘗試使用FriendlyUrlSegments的屬性來得到URL到控件的ID我得到這個錯誤:ASP.NET的WebForms FriendlyUrlSegments不包含一個構造函數參數0

public Person GetPerson([FriendlyUrlSegments]int? id) 
{ 
    return People.Find(p => p.Id == id); 
} 

我試圖更新:具有[FriendlyUrlSegments]錯誤

'Microsoft.AspNet.FriendlyUrls.ModelBinding.FriendlyUrlSegmentsAttribute' does not contain a constructor that takes 0 arguments 

方法來自NuGet的FriendlyUrls。

回答

2

寫你的方法,像這樣:

public Person GetPerson([FriendlyUrlSegments(0)] int? id) 
{ 
    return People.Find(p => p.Id == id); 
} 

[FriendlyUrlSegments]的屬性在URL頁面使用後段的零索引地圖。例如,如果您請求/ Hello/foo/bar/...並且您的頁面是Hello.aspx,那麼[FriendlyUrlSegments(0)]將映射到「foo」,[FriendlyUrlSegments(1)]將映射到「bar」,依此類推。

0

我認爲你做的方式是模型綁定不從URL獲取信息。 我讀了article

斯科特hanselman和我試試看。我和你有同樣的問題。我以爲[FriendlyUrlSegments]int? id

爲模型綁定。取而代之的是我這樣做:

var id=Request.GetFriendlyUrlSegments()[0]; 
yourmethod(id); 

請隨時糾正我,如果我錯了! 希望得到這個幫助。

相關問題