Glass Mapper various problems getting started

1,925 views
Skip to first unread message

Ben Rausch

unread,
Nov 7, 2013, 6:41:47 PM11/7/13
to glasssite...@googlegroups.com
Hi,

I'm just getting started with Glass Mapper so I created a simple project for a clean Sitecore 7 130918 installation. I'm using Sitecore MVC so I installed the Glass.Mapper.Sc.Razor and Glass.Mapper.Sc.Razor.CastleWindsor nuget packages. Straight out of the box I'm getting some errors in the App_Start\GlassMapperScRazor.cs file:

Some of the types can't be found so I went and had a look at the Glass.Mapper.Sc.Razor.CastleWindsor assembly in Reflector but it seems empty.

So I went and took the Glass.Mapper.Sc.CastleWindsor assembly and referenced it in my solution. Now the GlassMapperScRazor class has no errors. Using this assembly might be the reason why I'm running into my next error. Using TDS I pulled the Sample_Item out of Sitecore and generated the code for it. In my controller I'm now trying to do this to get my current context item:

It all compiles fine but when accessing the page in a browser I'm seeing an YSOD:

Server Error in '/' Application.

The given key was not present in the dictionary.

Description: An unhandled exception occurred. 

Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Source Error: 

Line 17:         public ActionResult Index()
Line 18:         {
Line 19:             var contextItem = new SitecoreContext().GetCurrentItem<Sample_Item>();
Line 20:             return View(contextItem);
Line 21:         }


Source File: c:\inetpub\wwwroot\USQ.Responsive\USQ.Responsive\USQ.Responsive.UI.Web\USQ\Websites\Responsive\Controllers\HomeController.cs    Line: 19 

Stack Trace: 

[KeyNotFoundException: The given key was not present in the dictionary.]
   System.Collections.Generic.Dictionary`2.get_Item(TKey key) +14178855
   Glass.Mapper.AbstractService..ctor(String contextName) +97
   Glass.Mapper.Sc.SitecoreService..ctor(Database database, String contextName) +55
   Glass.Mapper.Sc.SitecoreContext..ctor() +80
   USQ.Responsive.UI.Web.USQ.Websites.Responsive.Controllers.HomeController.Index() in c:\inetpub\wwwroot\USQ.Responsive\USQ.Responsive\USQ.Responsive.UI.Web\USQ\Websites\Responsive\Controllers\HomeController.cs:19
   lambda_method(Closure , ControllerBase , Object[] ) +113
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +261
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.<>c__DisplayClass13.<InvokeActionMethodWithFilters>b__10() +124
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +854290
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +854290
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +309
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +345


I've checked the WebActivator assemblies are in the bin folder of my website and that the GlassMapperScRazor.cs file in App_Start is running fine. 

Any ideas on what I'm doing wrong and how to fix this error?


Thanks
Ben

Michael Edwards

unread,
Nov 8, 2013, 7:03:13 AM11/8/13
to glasssite...@googlegroups.com
HI

You need to install the  Glass.Mapper.Sc.CastleWindsor nuget first, this will add some  WebActivator code into your project.

The Glass.Mapper.Sc.Razor.CaslteWindsor project is used to activate the Glass Razor project which is a rendering engine add on.

You should have the following under your App_Start:



With the GlassMapperSc class having the following:


The error you are getting is a result of the Glass Context not being created.

Mike



--
You received this message because you are subscribed to the Google Groups "Glass.Sitecore.Mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to glasssitecorema...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Ben Rausch

unread,
Nov 9, 2013, 5:50:42 PM11/9/13
to glasssite...@googlegroups.com, mikeed...@googlemail.com
Hi Mike,

thanks for your reply. I will give this a try tomorrow when I'm back in the office. I can't see the images (I assume you inserted images?) in your email though. Could you post them again to check my code looks correct?

Thanks
Ben

Ben Rausch

unread,
Nov 10, 2013, 6:35:07 PM11/10/13
to glasssite...@googlegroups.com
Hi Mike,

I have installed the Glass.Mapper.Sc.CastleWindsor nuget package into my web application. I have the following config files in app_start now:


I have debugged the startup code and it seem the files are executed like so: 

  1. GlassMapperScRazor
  2. GlassMapperSc

But when trying to run the application now I'm seeing this error:

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'Context' does not exist in the current context

Source Error:

Line 37:         protected ASP.global_asax ApplicationInstance {
Line 38:             get {
Line 39:                 return ((ASP.global_asax)(Context.ApplicationInstance));
Line 40:             }
Line 41:         }

Source File: c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\3571e9d0\9a57a0a8\App_Web_index.cshtml.2c11c10e.v_ycihzs.0.cs    Line: 39 



Any idea what I'm doing wrong now?

Thanks
Ben

Ben Rausch

unread,
Nov 10, 2013, 9:39:51 PM11/10/13
to glasssite...@googlegroups.com
Hi Mike,

I think I should add my view currently looks like this:

@inherits Glass.Mapper.Sc.Razor.Web.Ui.TypedTemplate<USQ.Responsive.Core.Sample.SampleItem>
<h1>Hello from HomeController</h1>
@Model.Text

This is giving me the error mentioned in my previous post. I changed the view to inherit from GlassView<SampleItem> and in this case I got:

Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 

Line 1:  @inherits Glass.Mapper.Sc.Web.Mvc.GlassView<USQ.Responsive.Core.Sample.SampleItem>
Line 2:  <h1>Hello from HomeController</h1>
Line 3:  @Model.Text

Source File: c:\inetpub\wwwroot\USQ.Responsive\Website\USQ\Websites\Responsive\Views\Home\Index.cshtml    Line: 


I hope this gives some more information about where my problem could be coming form.

Thanks
Ben
Reply all
Reply to author
Forward
0 new messages