Background
NET 4, C#, MVC3, using JsonFx, 以便序列化和淡化数据。 基地控制员已获准拦截所有要求,并采取以下行动:
- Get some JSON from a remote server.
- Run a LINQ query based on passed in keys (hero, body, footer).
- Return a generic model to the view.
This code works fine when running in a controller inheriting from the base controller, but when placed in the Base controller and called from an inherited controller, I get the following error:
Unable to cast object of type System.Linq.Expressions.ConstantExpression to type System.Linq.Expressions.ParameterExpression .
这是犯罪线:
var queryHero = heroModel.ArrayItems().Where(o => o.DisplayName == keyHero);
Question
在基地控制员时,我如何避免出现某种错误? 该法律还载有继承控制器的罚款。
Thanks for your time.
Full Code
public models.GenericPageModel GetGenericPageContent(string keyHero, string keyBody, string keyFooter)
{
try
{
// get content "tables"
var heroContent = GetJson("Page_Section_Hero_Content");
var bodyContent = GetJson("Page_Section_Body_Content");
var footerContent = GetJson("Page_Section_Footer_Content");
// new reader
var reader = new JsonReader(new DataReaderSettings(new DataContractResolverStrategy()));
// read it
var heroModel = reader.Query<models.PageSectionHeroContent>(heroContent);
var bodyModel = reader.Query<models.PageSectionBodyContent>(bodyContent);
var footerModel = reader.Query<models.PageSectionFooterContent>(footerContent);
// run query for current page
var queryHero = heroModel.ArrayItems().Where(o => o.DisplayName == keyHero);
var queryBody = bodyModel.ArrayItems().Where(o => o.DisplayName == keyBody);
var queryFooter = footerModel.ArrayItems().Where(o => o.DisplayName == keyFooter);
// models for return
models.PageSectionHeroContent hero;
models.PageSectionBodyContent body;
models.PageSectionFooterContent footer;
// any hero content?
if (queryHero.Any())
hero = new models.PageSectionHeroContent { DisplayName = queryHero.FirstOrDefault().DisplayName,
ContentXML = queryHero.FirstOrDefault().ContentXML };
else
hero = new models.PageSectionHeroContent { DisplayName = "Sorry, there was an error.",
ContentXML = "<details><error>No data was returned.</error></details>" };
// any body content?
if(queryBody.Any())
body = new models.PageSectionBodyContent { DisplayName = queryBody.FirstOrDefault().DisplayName,
ContentXML = queryBody.FirstOrDefault().ContentXML };
else
body = new models.PageSectionBodyContent { DisplayName = "Sorry, there was an error.",
ContentXML = "<details><error>No data was returned.</error></details>" };
// any footer content?
if(queryFooter.Any())
footer = new models.PageSectionFooterContent { DisplayName = queryFooter.FirstOrDefault().DisplayName,
ContentXML = queryFooter.FirstOrDefault().ContentXML };
else
footer = new models.PageSectionFooterContent { DisplayName = "Sorry, there was an error.",
ContentXML = "<details><error>No data was returned.</error></details>" };
// build generic page model
var model = new models.GenericPageModel { PageSectionHeroContent = hero, PageSectionBodyContent = body, PageSectionFooterContent = footer };
return model;
}
catch (Exception ex)
{
//todo: handle, log exception
return null;
}
}