创建CSV字符串的典型方法(伪代码):
代码示例:
public string ReturnAsCSV(ContactList contactList)
{
StringBuilder sb = new StringBuilder();
foreach (Contact c in contactList)
{
sb.Append(c.Name + ",");
}
sb.Remove(sb.Length - 1, 1);
//sb.Replace(",", "", sb.Length - 1, 1)
return sb.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我喜欢通过检查容器是否为空来添加逗号的想法,但这是否意味着更多的处理,因为它需要在每次出现时检查字符串的长度?
我觉得应该有一个更简单/更清洁/更有效的方法来删除最后一个逗号.有任何想法吗?
我们正在制定我们希望在开发团队中使用的设计指南,并在今天就如何命名ASP.NET控件进行讨论.我在谈论我们的好朋友Label,TextBox,Button等.
我们提出了以下三种投票方式:(示例是输入/显示FirstName的TextBox)
_
TextBox]或[FirstName _
tbx]我们最终决定使用选项2.它不像选项1那么冗长,我喜欢它指定控件名称之前的控件.
我的问题是微软是否已经发布了这些前缀的指南,或者如果您对我们的决定有任何意见.
我使用LINQ查询泛型字典,然后使用结果作为我的ListView(WebForms)的数据源.
简化代码:
Dictionary<Guid, Record> dict = GetAllRecords();
myListView.DataSource = dict.Values.Where(rec => rec.Name == "foo");
myListView.DataBind();
Run Code Online (Sandbox Code Playgroud)
我认为这会工作,但实际上它会抛出一个System.InvalidOperationException:
ID为'myListView'的ListView必须具有实现ICollection的数据源,或者如果AllowPaging为true,则可以执行数据源分页.
为了使它工作,我不得不采取以下措施:
Dictionary<Guid, Record> dict = GetAllRecords();
List<Record> searchResults = new List<Record>();
var matches = dict.Values.Where(rec => rec.Name == "foo");
foreach (Record rec in matches)
searchResults.Add(rec);
myListView.DataSource = searchResults;
myListView.DataBind();
Run Code Online (Sandbox Code Playgroud)
在第一个例子中是否有一个小问题使它工作?
(不知道该使用什么作为这个问题的标题,随意编辑更合适的东西)
我目前面临的任务是将自定义CMS实施中的大约200K项目导入Sitecore.我创建了一个简单的导入页面,它使用Entity Framework连接到外部SQL数据库,并创建了所有必需的数据模板.
在测试导入大约5K项目时,我意识到我需要找到一种方法来使导入运行更快,所以我开始寻找一些有关为此目的优化Sitecore的信息.我得出结论,那里没有太多具体信息,所以我想分享我发现的内容,并为其他人做出进一步的优化.我的目标是为Sitecore创建某种维护模式,可以在导入大量数据时使用.
我找到的最有用的信息是Mark Cassidy的博文http://intothecore.cassidy.dk/2009/04/migrating-data-into-sitecore.html.在这篇文章的底部,他提供了一些关于何时运行导入的提示.
我从这个列表中注意到的第一件事是BulkUpdateContext类,因为我从未听说过它.我很快就明白为什么在SND论坛和PDF文档中搜索没有回复.所以想象一下,当我实际测试它并发现它将项目创建/删除至少提高了十倍时,我感到惊讶!
接下来我要看的第一点是他基本上建议创建一个只包含执行导入所需的基本要素的Web配置版本.到目前为止,我已删除了与创建,保存和删除项目和版本相关的所有事件.我还从web配置中的master数据库元素以及任何自定义事件,日程表和搜索配置中删除了历史引擎和系统索引声明.我希望有很多其他东西我可以去删除/禁用以提高性能.管道?时间表?
你有什么优化技巧?
我们最近升级了我们工作的CMS,并且必须从Lucene.net V2.3.1.301升级到V2.9.4.1
我们在原始解决方案中使用了CustomScoreQuery,它使用内置查询无法实现各种过滤.(GEO,多日期范围等)
自从从旧版本迁移到新版本的Lucene后,它开始返回文档,即使我们检查结果时它们的分数为0甚至是负数
public LuceneTest()
{
Lucene.Net.Store.Directory luceneIndexDirectory = FSDirectory.Open(new System.IO.DirectoryInfo(@"C:\inetpub\wwwroot\Project\build\Data\indexes\all_site_search_en"));
Analyzer analyzer = new WhitespaceAnalyzer();
IndexSearcher searcher = new IndexSearcher(luceneIndexDirectory, true);
QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_23, "", analyzer);
parser.SetAllowLeadingWildcard(true);
Query dateQuery = ComposeEventDateQuery(new DateTime(2015, 11, 23), new DateTime(2015,11,25), searcher);
BooleanQuery combinedQuery = new BooleanQuery();
BooleanQuery.SetMaxClauseCount(10000);
combinedQuery.Add(dateQuery, BooleanClause.Occur.MUST);
TopDocs hitsFound = searcher.Search(dateQuery, 1000);
System.Console.WriteLine(String.Format("Found {0} matches with the date filters", hitsFound.TotalHits));
System.Console.ReadKey();
}
public static Query ComposeEventDateQuery(DateTime fromDate, DateTime ToDate, IndexSearcher MySearcher)
{
BooleanQuery query = new BooleanQuery(); …
Run Code Online (Sandbox Code Playgroud) 在我的网站上,我有四个自定义变量.我的问题是,谷歌分析由于某种原因只注册其中三个.页面上无法正常工作的脚本如下所示:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setCustomVar',3,'Category 3','some value']);
_gaq.push(['_setCustomVar',4,'Category 4','some value']);
_gaq.push(['_setAccount', 'UA-XXXXXXXX']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Run Code Online (Sandbox Code Playgroud)
此页面应该跟踪索引3和4中的两个自定义变量.另一个页面跟踪索引1和2中的自定义变量.
在Google Analytics中,我可以看到它已在前三个广告位中注册了类别(索引1-3),但第四个广告位中的类别从未注册过.根据文档,您最多可以有五个插槽.
任何人都可以解释为什么第四个变量永远不会被注册?
更新
检查分析请求中的utme变量提供了一些有趣的结果.
正在运行的第1页使用以下跟踪脚本:
_gaq.push(['_setCustomVar',1,'Category 1','value1']);
_gaq.push(['_setCustomVar',2,'Category 2','value2']);
Run Code Online (Sandbox Code Playgroud)
这导致以下utme参数:
8(Category 1*Category 2)9(value1*value2)
Run Code Online (Sandbox Code Playgroud)
第2页,它不起作用,使用以下跟踪脚本:
_gaq.push(['_setCustomVar',3,'Category 3','value3']);
_gaq.push(['_setCustomVar',4,'Category 4','value4']);
Run Code Online (Sandbox Code Playgroud)
这导致以下utme参数:
8(3!Category 3)9(3!value3)
Run Code Online (Sandbox Code Playgroud)
它显然忽略了我试图追踪的最后一个自定义值!
是否可以修改Sitecore中的"常规链接"字段,以便在模板字段定义中的"源"中使用查询?我希望可以添加一个管道处理器来处理General Link字段的查询,其方式与本文描述的为Treelists和Datasources启用查询的方式类似.
我通常将我的connectionstring存储在web.config或Visual Studio项目的应用程序设置中.我正在处理的应用程序会对数据库进行大量访问,这意味着它每次都会查找连接字符串.我应该将连接字符串放在缓存中还是应该将整个SqlConnection对象存储在缓存中以消除始终打开和关闭它们的需要?
更新:似乎共识是将连接字符串存储在配置文件中,并将缓存保留在ADO.NET的信任之中
如何在sitecore中检查当前用户是否为管理员?
就像是:
if(User.Current.Name == "extranet\Admin")
// then do some thing ??
Run Code Online (Sandbox Code Playgroud) 我有一个ASP.NET MVC5网站,我已经使用nuget安装了Glimpse.MVC5.该网站正在运行最新版本的Sitecore CMS(7.2 rev.140314).除了添加日志记录之外,我根本没有改变Glimpse配置.
当我去网站的主页(启用Glimpse后)我看不到HUD.以下是Glimpse日志中的最后一行:
2014-04-24 15:19:01.6043 | 调试| 显然GlimpseRuntime尚未初始化此请求.如果您正在执行此问题中提到的特定内容,可能会发生这种情况:https: //github.com/Glimpse/Glimpse/issues/703.无论哪种方式,Glimpse都将被禁用,以防止在此请求期间出现任何进一步的非确定性行为.
我看过链接,但我看不出它适用于我的情况.日志中没有NullReferenceException.
我已经在我的机器上测试了Glimpse.MVC5和一个vanilla ASP.NET MVC5网站,并且工作正常,所以我很想假设它与Sitecore有关.
任何想法可能是错误的或如何识别问题的想法?
是否可以创建NUnit Test方法来检查方法是否返回预期的数据类型?
这就是我的意思:
我有一个静态字符串,它接受两个参数并检查它是否与另一个字符串匹配.如果是,方法只返回该字符串.
我想测试以确保此方法确实返回字符串类型和可能发生的任何异常.
示例代码:
public static string GetXmlAttributeValue(this XmlElement element, string attributeName)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
if (attributeName == null)
{
throw new ArgumentNullException("attributeName");
}
string attributeValue = string.Empty;
if (element.HasAttribute(attributeName))
attributeValue = element.Attributes[attributeName].Value;
else
throw new XmlException(element.LocalName + " does not have an attribute called " + attributeName);
return attributeValue;
}
Run Code Online (Sandbox Code Playgroud)
以下是我的解决方案的样子:
我想在TestLibrary
类库中编写测试代码.
免责声明:我对ServiceStack很新
鉴于以下服务:
public class TestService : Service
{
public TestResponse Get(Test request)
{
if (String.IsNullOrEmpty(request.Message))
throw new ArgumentException("Message is required");
return new TestResponse();
}
}
Run Code Online (Sandbox Code Playgroud)
和以下请求/响应DTO:
[Route("/test", "GET")]
public class Test : IReturn<TestResponse>
{
public string Message { get; set; }
}
public class TestResponse
{
public IList<Test> TestList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我尝试访问/测试时,我希望得到以下响应:
{
"responseStatus": {
"errorCode": "ArgumentException",
"message": "Message is required",
"errors": []
}
}
Run Code Online (Sandbox Code Playgroud)
相反,我得到一个空的JSON响应.然而,它返回正确的状态代码(400 Bad Request).
我认为在ServiceStack中使用Something然后SomethingResponse以这种方式命名您的DTO是很常见的.为了让它将异常作为序列化的ResponseStatus对象返回,我发现我可以将我的请求DTO从Test重命名为TestRequest和/或在我的响应DTO中包含ResponseStatus属性.
这是预期的行为吗?
更新
我应该包括只有在我的响应DTO的名称以Response结尾(区分大小写)时才会出现问题.如果我的请求/响应DTO分别被称为Foo和Bar,我会收到错误的格式正确的JSON响应.
有一个布局页面,并尝试静态地放置一个视图渲染.并且还需要将数据源传递给它,就像这样
@Html.Sitecore().ViewRendering("/Views/Renderings/Components/LightboxModal.cshtml", new { DataSource = "/sitecore/content/Common Content/Medicare Disclaimer" });
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误,
传递到字典中的模型项的类型为"Sitecore.Mvc.Presentation.RenderingModel",但此字典需要"Web.Data.Models.LightboxModel"类型的模型项.
有人为此解决这个问题吗?
谢谢
sitecore ×6
c# ×5
asp.net ×2
coding-style ×1
csv ×1
generics ×1
glass-mapper ×1
glimpse ×1
linq ×1
listview ×1
lucene ×1
lucene.net ×1
nunit ×1
servicestack ×1
sitecore-mvc ×1
sitecore6 ×1
sitecore7 ×1
sitecore7.2 ×1