小编Rea*_*ion的帖子

你调用的对象是空的。(HttpContext.Current.Request)

我在我的自定义类中收到此错误。代码如下,我突出显示了出现错误的行,并且我已经检查了 cookie 是否存在:

static private Dictionary<string,string> KeyValueGet()
{
    Dictionary<string, string> ArrKeyVal = new Dictionary<string, string>();
    NameValueCollection CookieData = new NameValueCollection();
    **if (HttpContext.Current.Request.Cookies["CartData"].Values != null)**
    {
        CookieData = HttpContext.Current.Request.Cookies["CartData"].Values;
        string[] CookieKeys = CookieData.AllKeys;
        foreach (string s_key in CookieKeys)
        {
            ArrKeyVal.Add(s_key, CookieData[s_key]);
        }
    }
    return ArrKeyVal;
}
Run Code Online (Sandbox Code Playgroud)

更新:我添加了一个 If 语句来检查“null”,它甚至没有通过这个,我在 if 语句中得到相同的异常,看起来它无法处理 HttpContext.Current.Request 是什么。

任何意见都会受到赞赏。

c# asp.net

2
推荐指数
1
解决办法
5285
查看次数

Javascript函数有(文档)最后打了?

我正在尝试编写一个简单的Facebook应用程序.在我需要的代码中,我发现了这个:

  // Load the SDK asynchronously
  (function(d){
   var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
   if (d.getElementById(id)) {return;}
   js = d.createElement('script'); js.id = id; js.async = true;
   js.src = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));
Run Code Online (Sandbox Code Playgroud)

为什么这个函数被包裹在()中并且(文档)最后被打了?我以前没见过这种JavaScript法术.

感谢您的任何意见.

javascript

2
推荐指数
1
解决办法
121
查看次数

模型匹配此JSON用于反序列化,字段名称用短划线

我正在尝试创建一个与此JSON结构的JSON.NET deserealization相匹配的模型:第一项......

    {
    "190374": {
    "vid": 190374,
    "canonical-vid": 190374,
    "portal-id": 62515,
    "is-contact": true,
    "profile-token": "AO_T-mN1n0Mbol1q9X9UeCtRwUE1G2GFUt0VVxCzpxUF1LJ8L3i75x9NmhIiS0K9UQkx19bShhlUwlIujY4pSXAFPEfDG-k9n8BkbftPw6Y5oM3eU5Dc_Mm-5YNJTXiWyeVSQJAN_-Xo",
    "profile-url": "https://app.hubspot.com/contacts/62515/lists/public/contact/_AO_T-mN1n0Mbol1q9X9UeCtRwUE1G2GFUt0VVxCzpxUF1LJ8L3i75x9NmhIiS0K9UQkx19bShhlUwlIujY4pSXAFPEfDG-k9n8BkbftPw6Y5oM3eU5Dc_Mm-5YNJTXiWyeVSQJAN_-Xo/",
    "properties": {
    "phone": {
    "value": "null"
    },
    "hs_social_linkedin_clicks": {
    "value": "0"
    },
    "hs_social_num_broadcast_clicks": {
    "value": "0"
    },
    "hs_social_facebook_clicks": {
    "value": "0"
    },
    "state": {
    "value": "MA"
    },
    "createdate": {
    "value": "1380897795295"
    },
    "hs_analytics_revenue": {
    "value": "0.0"
    },
    "lastname": {
    "value": "Mott"
    },
    "company": {
    "value": "HubSpot"
    }
    },
    "form-submissions": [],
    "identity-profiles": []
    },
     "form-submissions": [],
    "identity-profiles": []
    },
**next similar entry**
Run Code Online (Sandbox Code Playgroud)

然后我尝试绑定此列表:

public …
Run Code Online (Sandbox Code Playgroud)

c# json model json.net

2
推荐指数
1
解决办法
1471
查看次数

使用LINQ的XML到下拉列表

下面的代码在没有这一行的情况下完美运行:".插入(0,新的SelectListItem(){Text ="",Value ="",Selected = false})"......基本上我试图在空白处添加一个空白值下拉列表,我在这行中得到的错误是"无法从void转换为SelectListItem".有关如何正确添加空白的任何想法?

@Html.DropDownListFor(model => model.itemPost.txtVal,
    new List<SelectListItem>(
        (from node in
             System.Xml.Linq.XDocument.Parse(Model.itemQuestionRowDef.SupportingXML).Descendants("option")
         select new SelectListItem
             {
                 Text = node.Value,
                 Value = node.Attribute("value").Value,
                 Selected = node.Attribute("value").Value == Model.itemPost.txtVal ? true : false
             }
         )
        ).Insert(0, new SelectListItem(){Text = "", Value="", Selected = false}),
    new { @class = "tobeFilled", id = "id_" + Model.counter, Name = "questionLst[" + Model.counter + "]." + "txtVal" })
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc

1
推荐指数
1
解决办法
100
查看次数

异步导致调试器跳转

我有这个代码:

private async Task<DataSharedTheatres.TheatresPayload> GetTheatres()
{
    var callMgr = new ApiCallsManager();
    var fileMgr = new FileSystemManager();

    string cachedTheatres = fileMgr.ReadFile(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TheatreTemp.txt"));
    if (string.IsNullOrEmpty(cachedTheatres))
    {
        **string generalModelPull = await callMgr.GetData(new Uri("somecrazyapi.com/api" + apiAccessKey));**
        bool saveResult = fileMgr.WriteToFile(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TheatreTemp.txt"), generalModelPull);
        if (!saveResult)
        {
            testText.Text = "Failed to load Theatre Data";
            return null;
        }
        cachedTheatres = fileMgr.ReadFile(Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "TheatreTemp.txt"));
    }
    return Newtonsoft.Json.JsonConvert.DeserializeObject<DataSharedTheatres.TheatresPayload>(cachedTheatres);
**}**
Run Code Online (Sandbox Code Playgroud)

我在第一个突出显示的行(它命中)上设置了断点,然后按F10,调试器跳转到最后一个括号!我不明白为什么.

GetData方法:

public async Task<string> GetData(Uri source)
{
    if (client.IsBusy) 
        client.CancelAsync ();
    string result = await client.DownloadStringTaskAsync (source);
    return result;


}
Run Code Online (Sandbox Code Playgroud)

c# asynchronous

1
推荐指数
1
解决办法
660
查看次数

LINQ对象性能

我正在构建一个关于DataSet的LINQ查询,它将具有.Where条件,.Distinct和.OrderBy.我一开始想手动执行此操作,最多我可以在两个循环中管理它.哪种方法更快?无论如何看看LINQ在后台做了什么?

c# linq

0
推荐指数
1
解决办法
348
查看次数

不可预知的JSON结构

我收到的JSON结构与第三方API有些不可预测.例如,我开始使用这样的类:

public Class UserTuple
{
    public int uid {get; set;}
    public String email {get; set;}
    public Dictionary<string,int> stats {get; set;}
    //Unknown structure here (Although I know its name)...
}
Run Code Online (Sandbox Code Playgroud)

我可以为此创建一个类,但它将完全嵌套...我不关心这部分的值,所以它似乎是浪费.有没有办法让JSON.NET知道忽略这个未知部分?

PS:我以这种方式反序列化:

JsonConvert.Deserialize<List<UserTuple>>(receivedJSON);
Run Code Online (Sandbox Code Playgroud)

c# json json.net

-1
推荐指数
1
解决办法
95
查看次数

标签 统计

c# ×6

json ×2

json.net ×2

linq ×2

asp.net ×1

asp.net-mvc ×1

asynchronous ×1

javascript ×1

model ×1