我有几个动作方法与IList类型的参数.
public ActionResult GetGridData(IList<string> coll)
{
}
Run Code Online (Sandbox Code Playgroud)
默认行为是当没有数据传递给action方法时参数为null.
有没有办法获得一个空集合而不是null应用程序?
我有 ASP.NET MVC 应用程序,我在其中注册了一个具有InstancePerHttpRequest作用域的组件。
builder.RegisterType<Adapter>().As<IAdapter>().InstancePerHttpRequest();
Run Code Online (Sandbox Code Playgroud)
然后我有一段异步代码,我正在解析适配器组件。
下面的代码是简化的
Task<HttpResponseMessage> t = Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>
// IHandleCommand<T> takes an IAdapter as contructor argument
var h = DependencyResolver.Current.GetServices<IHandleCommand<T>>();
);
Run Code Online (Sandbox Code Playgroud)
上面的代码抛出异常: The request lifetime scope cannot be created because the HttpContext is not available.
所以我对这个主题做了一些研究,找到了这个答案 /sf/answers/606411501/
然后我将解析代码调整为此
using (var c= AutofacDependencyResolver.Current.ApplicationContainer.BeginLifetimeScope(x => x.RegisterType<DataAccessAdapter>().As<IDataAccessAdapter>).InstancePerLifetimeScope()))
{
var h = DependencyResolver.Current.GetServices<IHandleCommand<T>>();
}
Run Code Online (Sandbox Code Playgroud)
但例外情况保持不变。 The request lifetime scope cannot be created because the HttpContext is not available.
我错过了什么吗?
我想为我的js,css和html文件设置不同的缓存控制头值.我知道在每个文件夹的基础上设置它的选项,但我的应用程序在同一文件夹中有html和js文件.
它甚至可以在IIS中使用吗?
我正在尝试使用azure-storage上传流,但该方法CreateBlockBlobFromStream需要流长度.我不知道在哪里长度.
我的代码
const Readable = require('stream').Readable;
const rs = Readable();
rs._read = () => {
//here I read a file, loop through the lines and then generate some xml
};
const blobSvc = azure.createBlobService(storageName, key);
blobSvc.createBlockBlobFromStream ('data','test.xml', rs, ???, (err, r, resp) => {});
Run Code Online (Sandbox Code Playgroud) 我正在尝试捕获其他应用程序的输出.捕获ping的输出效果很好.变量输出包含预期输出.
var p = new Process();
p.StartInfo.FileName = "ping";
p.StartInfo.Arguments = "www.google.com";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
var output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
但是,当我使用此代码捕获expdp的输出(这是导出的oracle工具)时,该变量为空.在控制台中运行相同的命令将返回一些输出.
p.StartInfo.FileName = "expdp";
p.StartInfo.Arguments = "help=y";
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?
我在项目中添加了NLog.按照说明我创建了NLog.config.
<?xml version="1.0" ?>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="file" xsi:type="File"
layout="${longdate} ${logger} ${message}"
fileName="${basedir}/${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="file" />
</rules>
</nlog>
Run Code Online (Sandbox Code Playgroud)
然后只记录一些东西.
var logger = LogManager.GetCurrentClassLogger();
logger.Info("xxxx");
Run Code Online (Sandbox Code Playgroud)
使用开发人员Web服务器它可以正常工作,但是当我将应用程序发布到IIS时,不会创建任何日志.
我们有一个模型
public class Model
{
public int Number { get; set; }
public DateTime Date { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我观察到以下行为.如果没有提交任何值,则ModelState.IsValid的值为true,则Number属性的值为0,Date的值为1.1 0001 00:00:00.
DataAnnotationsModelValidatorProvider类和GetValidators方法具有此代码片段
// Add an implied [Required] attribute for any non-nullable value type,
// unless they've configured us not to do that.
if (AddImplicitRequiredAttributeForValueTypes &&
metadata.IsRequired &&
!attributes.Any(a => a is RequiredAttribute)) {
attributes = attributes.Concat(new[] { new RequiredAttribute() });
}
Run Code Online (Sandbox Code Playgroud)
如果我理解这一点,那么Number属性和DateTime属性应该设置一个RequiredAttribute,验证例程应该设置模型无效并生成相应的错误消息.
所以我的问题是为什么模型无效?
我正在使用ASP.NET MVC 3
我正在尝试为我的IoC容器(autofac)注册RequestContext.我在Application_start中进行所有注册.
RequestContext注册如下所示:
builder.Register(x => HttpContext.Current.Request.RequestContext).As<RequestContext>();
Run Code Online (Sandbox Code Playgroud)
这在dev webserver上工作正常,但在IIS 7(集成模式)中,问题是Application_start中没有RequestContext上下文.
我能在这做什么?
我想用于我的下一个项目Ext js和ASP.NET MVC.
我想知道将这两个框架结合使用的最佳方式是什么.到目前为止,我使用ASP.NET MVC做了一些项目,其中每个操作方法返回一个视图并重新加载页面.Ext js mvc应用程序使用单页方法.
因为我对ext js很新,所以我想知道是否有人可以分享使用这两个框架构建现实世界应用程序的一些经验.
我有这个代码从json字符串中获取值.
var json = @"[{""property"":""Status"",""value"":""val""}]";
var jArray = JArray.Parse(json);
foreach (JToken jToken in jArray)
{
var property = jToken.Value<string>("property");
var value = jToken.Value<string>("value");
}
Run Code Online (Sandbox Code Playgroud)
这适用于提供的输入.但在某些情况下,value属性可能包含一个数组.
var json = @"[{""property"":""Status"",""value"":[1,2]}]";
Run Code Online (Sandbox Code Playgroud)
我想以某种方式检查值是否包含简单值或数组.如果值是数组,则将其绑定到集合.
这可能使用JSON.net吗?
是否可以将此if语句转换为单行语句?
if (value != DBNull.Value)
{
dic.Add(columnName);
}
else if (!skipNullValues)
{
dic.Add(columnName);
}
Run Code Online (Sandbox Code Playgroud) 我需要为以下场景创建一个正则表达式.
它只能有数字,只有一个点或逗号.
第一部分可以有一到三个数字.第二部分可以是点或逗号.第三部分可以有一到两个数字.
有效的方案是
123,12
123.12
123,1
123
12,12
12.12
1,12
1.12
1,1
1.1
1
Run Code Online (Sandbox Code Playgroud)
到目前为止,我带着这个表情走了过来
\d{1,3}(?:[.,]\d{1,2})?
Run Code Online (Sandbox Code Playgroud)
但它效果不好.例如,输入为11:11被标记为有效.
我想将两个项目数组合并为一个.我使用Exp.apply方法.但结果只包含第二个数组中的项目.
样品
预期的结果应该是
{ items:[{ name:'xxx' }, { name2:'yyy'}]})
Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×5
c# ×4
autofac ×2
extjs ×2
azure ×1
if-statement ×1
iis ×1
json.net ×1
nlog ×1
node.js ×1
refactoring ×1
regex ×1
stream ×1
validation ×1