Resolving BlogPostService from a WebPages CSHTML file

Topics: General
Oct 6, 2011 at 12:37 PM

Hi

var blogService = new Orchard.Blogs.Services.BlogPostService();     

How can I resolve all the dependencies for BlogPostService from within a WebPages CSHTML file?

I have a default install of Orchard 1.3 and a file Blog.cshtml in the root. I wish to query against the BlogPostService from this file inline ie. Razor WebPages type page.

I'm not wanting to write a module, theme or similar, but just access the Orchard API outside of a module and outside of an MVC controller.

Any pointers?

Cheers.

Andy

 

<!DOCTYPE html>

<html>   

<head>   

 <title>WebPages</title>   

</head>   

<body>     

 @{           

var blogService = new Orchard.Blogs.Services.BlogPostService();     

 }  

</body>

</html>

Oct 6, 2011 at 12:57 PM
Hi,

Try using ContentManager.Resolve<>.

Thanks

Steve



On 6 Oct 2011, at 13:37, "andyboothuk" <notifications@codeplex.com> wrote:

From: andyboothuk

Hi

var blogService = new Orchard.Blogs.Services.BlogPostService();

How can I resolve all the dependencies for BlogPostService from within a WebPages CSHTML file?

I have a default install of Orchard 1.3 and a file Blog.cshtml in the root. I wish to query against the BlogPostService from this file inline ie. Razor WebPages type page.

I'm not wanting to write a module, theme or similar, but just access the Orchard API outside of a module and outside of an MVC controller.

Any pointers?

Cheers.

Andy

<!DOCTYPE html>

<html>

<head>

<title>WebPages</title>

</head>

<body>

@{

var blogService = new Orchard.Blogs.Services.BlogPostService();

}

</body>

</html>

Oct 6, 2011 at 1:54 PM

Actually, I think you want to use WorkContext, not ContentManager. The Orchard WorkContext is available in any Orchard view, so you can do something like this in your view:

@{
    var blogPostService = WorkContext.Resolve<IBlogPostService>();
}

Oct 6, 2011 at 2:34 PM

Hi Steve and Kevin

It's still a little tricky getting WebPages to work. I've not got it working yet.

WorkContext.Resolve does look promising. However, how do I get the WorkContext from a Razor WebPage (as opposed to an Orchard Mvc Razor View)?

For reference, I have made the following root Web.config changes. Without them WebPages simply do not get compiled and run.

Cheers.

Andy

 

Add:

<appSettings>   

<add key="webpages:Enabled" value="true" />

</appSettings>

Change from:

     <system.web.webPages.razor>        <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />        <pages pageBaseType="Orchard.Mvc.ViewEngines.Razor.WebViewPage">            <namespaces>                <add namespace="System.Web.Mvc" />                <add namespace="System.Web.Mvc.Ajax" />                <add namespace="System.Web.Mvc.Html" />                <add namespace="System.Web.Routing" />                <add namespace="System.Linq" />                <add namespace="System.Collections.Generic" />                <add namespace="Orchard.Mvc.Html" />            </namespaces>        </pages>    </system.web.webPages.razor>

Change to:

  <system.web.webPages.razor>    <pages pageBaseType="System.Web.WebPages.WebPage">      <namespaces>      <add namespace="System.Linq" />      </namespaces>    </pages>  </system.web.webPages.razor>

Oct 6, 2011 at 2:51 PM

Oh, I didn't realize you were trying to use WebPages for this. I'm afraid I don't know how you could do that. Hopefully somebody else will have some ideas. I have to ask though ... why are you wanting to do that instead of just using Orchard views?

Oct 6, 2011 at 3:00 PM

Hi Kevin

There are a couple of reasons why I'd like to use WebPages

[1] it could be a quick way to put together test pages of content managed data; in fact I'd assume you could, as Orchard is supposed to play nice with WebMatrix, which typically uses WebPages

[2] the WebPages could be other light weight pages based on small amounts of content managed data eg. a Facebook Tab Application

[3] to bypass all the arguably redundant bootstrapping, modules, shapes and routes that Orchard imposes, but still manage the site from the Admin area

[4] to bypass all the markup conventions and div wrappers and focus on lean mean HTML5

Andy

Oct 6, 2011 at 5:06 PM
Yeah. Sorry that's what I meant.

I don't think Webmatrix is supported in 1.3 but an update is coming soon I believe.

S

On 6 Oct 2011, at 14:54, "kevink" <notifications@codeplex.com> wrote:

From: kevink

Actually, I think you want to use WorkContext, not ContentManager. The Orchard WorkContext is available in any Orchard view, so you can do something like this in your view:

@{
    var blogPostService = WorkContext.Resolve<IBlogPostService>();
}

Oct 6, 2011 at 7:58 PM

Hi Andy,

I guess I would still just question the reason for doing that. Seems like you're trying to swim upstream when you could just go with the flow. :-)

Orchard does play nice with WebMatrix. WebPages is just one of the frameworks that WebMatrix can work with. PHP is another, but that doesn't mean it necessarily makes sense to try to mix PHP and Orchard. Orchard is an ASP.Net MVC 3 framework, not a WebPages framework. They both use Razor but they're a bit different.

It sounds to me like you're trying to make use of the content management features of Orchard, so you might as well just use Orchard. You don't really have to concern yourself with the "bootstrapping, modules, shapes and routes" if all you want to do is fire up a site in WebMatrix and add some pages from the admin area. Also, Orchard does focus on HTML5, and it's not very hard to change the markup and wrappers to suit your taste if you want to do that.

Anyway, just some things to think about. I just think you might be trying to fit a square peg into a round hole, when you already have a perfectly good round peg to work with. :-)

Oct 6, 2011 at 8:27 PM

Hi Kevin

Any further pointers on how to use Orchard's dependency resolution and core content management APIs outside of modules? Why wouldn't something simple using DependencyResolver work?

DependencyResolver.Current.GetService<IBlogPostService>()

My main requirement is not content management - it's apps. This is an exploration of what content management elements of Orchard I can use outside of Orchard conventions to supplement a campaign app eg. edit the terms and conditions, update news items. Whilst the core of the project is a custom campaign app.

Andy

Oct 6, 2011 at 8:38 PM

Hi Andy,

In my opinion, that's just the thing I think you might be missing. Orchad is an app framework built on MVC 3. Content management is one of the services it provides, but it's more than that. It's a good platform for building any kind of web app on .Net, IMO.

But as for your specific question, I'm afraid I don't have an answer. Perhaps somebody else will. I really don't know how that would work when your pages are not part of the Orchard pipeline.

Kevin

Coordinator
Oct 6, 2011 at 8:46 PM

Orchard was never designed for that sort  of scenario and I don't think it will work without some very heavy lifting.