문제

The title pretty much sums it up. I created my own module in Orchard. I can access its actions via http requests like an ordinary controller in any MVC application.

I generated the controller using the command line interface and it came with IOrchardServices property that is populated in the constructor.

On my Orchard site I have a blog which I filled up with about 40 blog posts. How can I query these blog posts from within my controller?

도움이 되었습니까?

해결책

First I would like to start by saying: "read the source luke". You may find Orchard lacking in documentation and examples, but because it is open source, pretty much everything you want to know can be found there.

You should use the BlogPostService, inject that into your controller to get the blog posts you want.

You can see it being used in several controllers within Orchard.Blogs:

Check out the code in the BlogPostService to see how it works, it is a little confusing because blogs are content items with blog posts underneath them. If you want to learn about simpler querying of content items I would check out how the BlogService works, it is a little easier to get to grips with:

다른 팁

Here is an example of what I was looking for:

var query = Services.ContentManager.Query();

var list = query.ForType(new []{"BlogPost"}).List();

var result = new List<dynamic>();
foreach (var contentItem in list) {
    result.Add(new
    {
        title = contentItem.As<TitlePart>().Title, // dynamically typed: ((dynamic)contentItem).TitlePart.Title
        text = contentItem.As<BodyPart>().Text
    });
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top