English 中文(简体)
MVC3 Base Contoller Crashes, Same Code Works in Inherited Chief
原标题:MVC3 Base Contoller Crashes, Same Code Works in Inherited Controller

Background

NET 4, C#, MVC3, using JsonFx, 以便序列化和淡化数据。 基地控制员已获准拦截所有要求,并采取以下行动:

  1. Get some JSON from a remote server.
  2. Run a LINQ query based on passed in keys (hero, body, footer).
  3. 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;
    }
}
最佳回答

确切地说,可以确定什么是“NOT”在上及时启动的,但找到了工作。

<>strong>DISCLAIMER: 我是像<条码>boy knownjib一样的团队,这一解决办法可能最适合我们的具体情况。 说到这一点后,我们感到解决办法值得仿效,因为它可能帮助那些遇到这种qui的人。

Solution

解决办法是使用专门针对C# 4.0和的<条码>类型和<代码>ExpandoObjects的组合。 IDictionary category.

 public models.GenericPageDataModel GetGenericPageContent(string keyHero, string keyBody, string keyFooter)
        {
            try
            {
                var pageData = new List<models.GenericPageData>
                             {
                                 new models.GenericPageData{Key = "HeroModel",Value = GetJson("pro_Page_Section_Hero_Content")},
                                 new models.GenericPageData{Key = "BodyModel",Value = GetJson("pro_Page_Section_Body_Content")},
                                 new models.GenericPageData{Key = "FooterModel",Value = GetJson("pro_Page_Section_Footer_Content")}
                             };

                var reader = new JsonReader();

                var model = new models.GenericPageDataModel();
                foreach (var p in pageData)
                {
                    var objList = new List<ExpandoObject>();
                    dynamic output = reader.Read(p.Value);
                    foreach (var h in output)
                    {
                        dynamic obj = new ExpandoObject();
                        foreach (dynamic o in h)
                        {
                            var item = obj as IDictionary<String, object>;
                            item[o.Key] = o.Value;
                        }
                        objList.Add(obj);
                    }
                    switch (p.Key)
                    {
                        case ("HeroModel"):
                            model.HeroModel = objList.FirstOrDefault(o => o.FirstOrDefault(i => i.Key.Equals("DisplayName")).Value.Equals(keyHero));
                            break;
                        case ("BodyModel"):
                            model.BodyModel = objList.FirstOrDefault(o => o.FirstOrDefault(i => i.Key.Equals("DisplayName")).Value.Equals(keyBody));;
                            break;
                        case ("FooterModel"):
                            model.FooterModel = objList.FirstOrDefault(o => o.FirstOrDefault(i => i.Key.Equals("DisplayName")).Value.Equals(keyFooter));;
                            break;
                        default:
                            break;
                    }
                }
                return model;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

Supporting Models

public class GenericPageData
        {
            public string Key { get; set; }
            public string Value { get; set; }
        }

        public class GenericPageDataModel
        {
            public ExpandoObject HeroModel { get; set; }
            public ExpandoObject BodyModel { get; set; }
            public ExpandoObject FooterModel { get; set; }
        }

Razor Usage

@model my.project.com.Models.ContentModels.GenericPageDataModel        

@{
    ViewBag.Title = "My Title";
    Layout = "~/Views/Shared/_Layout.cshtml";

    ViewBag.heroDisplayName = @Model.HeroModel.ToDictionary(p => p.Key).FirstOrDefault(k => k.Key.Equals("DisplayName")).Value.Value;
}

<h1> @ViewBag.heroDisplayName</h1>
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签