我有一个像这样的参数列表:
public class parameter
{
public string name {get; set;}
public string paramtype {get; set;}
public string source {get; set;}
}
IEnumerable<Parameter> parameters;
Run Code Online (Sandbox Code Playgroud)
我想要检查它的一系列字符串.
string[] myStrings = new string[] { "one", "two"};
Run Code Online (Sandbox Code Playgroud)
我想迭代参数列表并检查source属性是否等于任何myStrings数组.我可以用嵌套的foreach来做到这一点,但我想学习如何以更好的方式做到这一点,因为我一直在玩linq,就像可枚举的扩展方法一样,所以嵌套的foreachs只是感觉不对.有没有更优雅的首选linq/lambda/delegete方法来做到这一点.
谢谢
我想知道是否有一个简单的方法来获得异步httpwebrequest的响应.
我已经在这里看到了这个问题但是我试图做的就是将字符串形式的响应(通常是json或xml)返回到另一个方法,然后我可以解析它/相应地处理它.
下面是一些代码:
我在这里有这两个静态方法我认为是线程安全的,因为所有的参数都被传入并且方法没有共享的局部变量?
public static void MakeAsyncRequest(string url, string contentType)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = contentType;
request.Method = WebRequestMethods.Http.Get;
request.Timeout = 20000;
request.Proxy = null;
request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
}
private static void ReadCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
try
{
using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult))
{
Stream responseStream = response.GetResponseStream();
using (StreamReader sr = new StreamReader(responseStream))
{
//Need to return this response
string strContent = sr.ReadToEnd();
}
}
manualResetEvent.Set();
}
catch (Exception ex)
{ …Run Code Online (Sandbox Code Playgroud) 我有这样的域类:
public class DomainClass
{
public virtual string name{get;set;}
public virtual IList<Note> Notes{get;set;}
}
Run Code Online (Sandbox Code Playgroud)
我该如何从中删除项目IList<Note>?如果它是一个List我可以做到,但它必须是一个,IList因为我使用Nhibernate作为我的持久层.
理想情况下,我想在我的域类中使用这样的方法:
public virtual void RemoveNote(int id)
{
//remove the note from the list here
List<Note> notes = (List<Note>)Notes
notes.RemoveAll(delegate (Note note)
{
return (note.Id = id)
});
}
Run Code Online (Sandbox Code Playgroud)
但我不能把它IList作为一个List.这周围有更优雅的方式吗?
您好我正在使用类似这样的HttpClient:
public static Task<string> AsyncStringRequest(string url, string contentType)
{
try
{
var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(contentType));
return client.GetStringAsync(url).ContinueWith(task => {
return task.Result;
});
}
catch (AggregateException ex)
{
throw ex;
}
catch (WebException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
}
Run Code Online (Sandbox Code Playgroud)
但我处理异常时遇到困难.我添加了额外的catch块只是为了尝试并逐步抛出,但没有任何断点被捕获到任何catch块中.我意识到使用Task,异常可能发生在与调用者不同的线程上,因此异常包含在聚合容器中,但我不确定处理这些异常的最佳方法是什么.
例如,我向Web服务发出请求,并在请求中指定了特定的无效参数,并抛出异常.我希望我能够捕获聚合异常,并查看innerexceptions以找出请求失败的原因并返回友好消息.
所以我的问题是,捕获这些聚合异常并处理它们的最佳方法是什么?
我需要从json响应中选择一些值.我使用json.net,更简单的东西很好,但似乎没有太多文档/教程在过去的任何事情.在下面的json示例中,我需要选择所有年龄:
{
"teacherHolder": [{
"id": 200000001,
"name": "Mr Test",
"class": "a4",
"students": [{
"id": "100532469",
"name": "ben"
},
{
"id": "100506025",
"name": "bill"
},
{
"id": "100000447",
"name": "bob"
}]
}]
Run Code Online (Sandbox Code Playgroud)
}
我尝试了这个和其他变化:
var stuff = response["teacherHolder"].Children()["students"];
var names = from y in stuff.Children().Values()
select y["name"];
Run Code Online (Sandbox Code Playgroud)
还有这个:
var names= response["teacherHolder"]
.Select(s => (string)s.SelectToken("students[0].name")).ToList();
Run Code Online (Sandbox Code Playgroud)
响应是来自webrequest的JObject.我回过头来看看:
[{"Key":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]","Value":"Newtonsoft.Json.Linq.JEnumerable`1[Newtonsoft.Json.Linq.JToken]"}]
Run Code Online (Sandbox Code Playgroud)
结果最终被放入字典中.
知道怎么做吗?我知道这很简单,我还没找到合适的组合.
我创建了一个过滤器,它继承了asp.net web api中的System.Web.Http.Filters.ActionFilterAttribute,并希望访问HttpActionExecutedContext结果对象中的一些数据.
在什么阶段/什么时候这个对象被填充?当我重写OnActionExecuted方法时它看着它并且它总是为空?
有任何想法吗?
编辑:
例如,在我的自定义过滤器中:
public override OnActionExecuted(HttpActionExecutedContext context)
{
//context.Result.Content is always null
base.OnActionExecuted(context);
}
Run Code Online (Sandbox Code Playgroud) 我一直在研究学习依赖注入(我认为我现在已经掌握了基础知识),并且我希望将它实现到webform应用程序中.
我的问题是,我应该为webforms项目使用什么依赖注入框架,还是一个最适合你的问题?
我目前看过Spring.Net,Ninject,Unity和StructureMap,我倾向于在配置中没有偏好,无论是XML还是流畅的界面.然而,XML配置变得不那么有利了吗?
我遇到的大部分信息都与MVC环境中的依赖注入有关.并且还了解到一些框架(例如Structure Map)仅适用于使用2.0版或更早版本的webforms. 所以我需要考虑的事情是webforms是否会持续支持,以及为模式相对较新的人配置的简易性.
先感谢您.
要求js在没有捆绑的情况下正常工作.但每当我使用捆绑包时,我都会为我要导入的模块获得超时.
以下是我使用asp.net mvc bundler/minifier构建软件包的方法
bundles.Add(new ScriptBundle("~/bundles/test").Include(
"~/scripts/jquery-{version}.js",
"~/scripts/bootstrap.js",
"~/scripts/moment.js"));
bundles.EnableOptimizations = true;
Run Code Online (Sandbox Code Playgroud)
Heres是cshtml文件中的require js config:
<script>
require.config({
baseUrl: "/scripts",
paths: {
jquery: "jquery-1.11.2"
},
waitSeconds: 20,
shim: {
bootstrap: {
deps: ['jquery']
}
},
bundles: {
'@Scripts.Url("~/bundles/test").ToString()': [
'jquery',
'bootstrap',
'moment'
]
}
});
require(["app/main/test"]);
</script>
Run Code Online (Sandbox Code Playgroud)
页面的js(app/main/test):
require(['jquery', 'moment'], function ($, moment) {
console.log(moment().format());
});
Run Code Online (Sandbox Code Playgroud)
Jquery,bootstrap和moment库都在我创建的测试包中,但是我加载页面暂时加载超时.
这是chrome检查器错误:

有任何想法吗?
提前致谢.
嗨,我正在努力与Dapper握手.
我的情况是我想从查询中将两个值拉入两个单独的字符串.我不确定我是否会以正确的方式解决这个问题,但这就是我正在做的事情:
string sql = @"Select type, name
FROM ZipData
WHERE Zip = @zip";
using (var multi = conn.QueryMultiple(sql, new { zip = zip }))
{
string result = multi.Read<string>().SingleOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
我得到了无法访问已处置的对象.对象名称:'GridReader'.当试图读取第二个字符串时.事情是它正确获取第一个值并且在我想要的读者中有两个字段.我确定我滥用api.
我在这做错了什么?我用谷歌搜索,但可以找到一个具体的例子.
嗨,我想尝试使用mvc 3来掌握ajax.beginform.
我有两个表单,一个HTML.BeginForm可以正常工作,一个Ajax.BeginForm在局部视图中.ajax表单的帖子是为模型添加注释.
问题是ajax提交是在主视图中提交包括HTML.BeginForm在内的整个页面而不是用note来执行异步帖子.
谁能看到我做错了什么?
我看了其他问题,并在web.config等中禁用了不显眼的javascript.
视图:
<div id="maincontent">
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Edit Franchise</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.FirstLine)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstLine)
@Html.ValidationMessageFor(model => model.FirstLine)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.SecondLine)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SecondLine)
@Html.ValidationMessageFor(model => model.SecondLine)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.City)
@Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
@Html.LabelFor(model => …Run Code Online (Sandbox Code Playgroud) 我正在尝试记录API的请求和响应.我目前正在使用delegatingHandler捕获httpwebresponses和httpwebrequests.
处理器:
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
LogRequest(request);
return base.SendAsync(request, cancellationToken).ContinueWith((task) =>
{
HttpResponseMessage response = task.Result;
LogResponse(response);
return response;
});
}
Run Code Online (Sandbox Code Playgroud)
LogResponse:
private void LogResponse(HttpResponseMessage response)
{
ApiResponse info = new ApiResponse();
//Populate response obj
if (response.Content != null)
{
response.Content.ReadAsStringAsync().ContinueWith((task) =>
{
info.Content = task.Result;
}
);
}
_repository.LogResponse(info);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将内容插入我的存储库中的数据库时,我得到一个null异常.但是当我逐步完成内容对象时,它会填充响应.我想我正在尝试在请求完成之前将数据插入到数据库中.
我不想等待它,因为这违背了使用异步请求的目的,如何在请求完成时将其挂钩以运行logresponse方法?我认为continueWith处理了这个并在任务完成时运行动作(这是否意味着下载请求不一定完成了?).
有任何想法吗?
我在我的应用程序中实现了一些请求限制.它基于使用MemoryCache和根据限制到期的项目.基本上我尝试检索一个缓存项目,如果它不在那里我继续发出请求,如果是他们我检查最后一个请求和锻炼下一个可用时间我可以提出请求.我的问题在于我必须等待提出请求.
//This is a shortened version of what im doing, but the includes the required code
public void ThrottleRequest(string webservice)
{
if (cache.TryGet(webservice))
{
var timeToWait = GetTimeToWaitFromSomewhere();
Wait(timeToWait);
}
}
public async void Wait(TimeSpan timeToWait)
{
await Task.Delay(timeToWait); //This doesnt work
Thread.Sleep(timeToWait); //This works
}
Run Code Online (Sandbox Code Playgroud)
问题在于等待方法.如果我使用thread.sleep数字匹配并且请求被正确限制(即每秒1个请求)但我永远不想在生产环境中使用它.那么在这里异步等待的正确方法是什么?我滥用API还是什么?
下面是场景:我有一个像这样的User对象:
public class User : BaseEntity<User>, IAggregateRoot
{
public virtual string Name { get; set; }
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string SecretQuestion { get; set; }
public virtual string SecretAnswer { get; set; }
public virtual DateTime LastLogin { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在编辑这个对象的过程中,我将它加载到视图中,但我只想更新一些属性(即我不想更新LastLogin属性).在这种情况下,我该怎么办?
是创建用户视图模型的最佳策略,当我尝试使用null LastLogin字段更新用户对象时,nhibernate是否应对此问题?
提前致谢.
编辑
像这样的东西:
public class UserViewModel
{
public string Name {get;set;}
public string UserName {get;set;}
public string Password {get;set;}
public …Run Code Online (Sandbox Code Playgroud) c# ×8
asynchronous ×3
asp.net-mvc ×2
list ×2
asp.net ×1
c#-4.0 ×1
compare ×1
dapper ×1
generics ×1
httpclient ×1
json ×1
json.net ×1
linq ×1
loops ×1
nhibernate ×1
requirejs ×1
rest ×1
sql-server ×1
subset ×1
task ×1
throttling ×1
timeout ×1
viewmodel ×1
webforms ×1