asp.net mvc action result and count question
I am working on a plugin based architecture. Its kind of social networking site. For a user, I can have pictures, blogs, videos. but they all come from modules. So, when you visit a user profile, you see tabs, saying pictures, videos and other things. I have an interface
public interface IUserContent
{
UserContent GetContent(long id);
}
public class UserContent
{
public int Count {get;set;}
public string URL {get;set;
public string Text {get;set;
}
So, if a photos module implements IUserContent it will look like this
publi开发者_StackOverflow中文版c class UserPhotos : IUserContent
{
public UserContent GetContent(long id)
{
return new UserContent {
Count = SomeRepository.GetPhotosCountByUser(id),
URL = string.format("/photos/byuser/{0}",id) ,
Text ="Photos" };
}
}
This URL is an action method in Photos Controller.
On my profile page i get all the implementations of IUserContent
var list = GetAllOf(IUserContent);
and render them using foreach loop as tabs on user profile page.
Now, my question is, is there any better way to do it. I dont want to use URL as string. Or any better practise to achieve it.
Regards
Parminder
Yes.
Replace URL = string.format("/photos/byuser/{0}",id)
with:
URL = Url.Action("ByUser", "Photo", new { id })
If you wish to generate a url whereby your action is called ByUser
and accepts an id
as argument and your controller is called PhotoController
; or use:
URL = Url.RouteUrl("PhotosByUser", new { id })
If you wish to generate your url derived from a route called PhotosByUser
.
See: -
- http://msdn.microsoft.com/en-us/library/dd460348.aspx and;
- http://msdn.microsoft.com/en-us/library/dd505215.aspx
For further information :)
精彩评论