小编Dan*_*son的帖子

使用数据库保存应用程序设置

我正在寻找使我们的应用程序更具可扩展性和更易于操作的方法,而无需更改 web.config (或者,在我们的例子中,是包含 appsettings 节点的 application.config 文件)。

我考虑过的一种方法是将应用程序设置保留在具有 sqlcachedependency 的数据库表中。这意味着:

  • 每当数据库中的设置发生更改时,缓存就会失效,并再次检索设置,从而实时更新应用程序,而无需更改文件并重新启动整个应用程序。
  • 我们可以创建一个自定义工具来更改设置。

我认为的缺点是,这可能会导致严重的逻辑问题,因为如果您有一些东西可以在流程开始时检查应用程序设置,然后它在中途发生变化,您最终可能会无意中改变流程,如无需重新启动完整的应用程序。

有办法解决这个问题吗?

是否有更好的方法来管理应用程序设置,以便您可以一次性远程远程更改一台、几台或所有服务器的应用程序设置?

c# database asp.net web-config appsettings

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

更正301重定向的控制器代码

我正在从静态站点设计一个新的动态站点.我的路线全部排序但我对我的Action方法有疑问.

下面是代码,但是当测试并查看Firebug报告的标头时,如果我取出Response.End它是302重定向我假设因为我设置了301然后调用另一个动作使它成为302,但如果我放入Response.End我得到301.

我猜测添加Response.RedirectLocation实际上正在进行301重定向,所以我是否将我的返回值更改为EmptyResult或null,即使该行代码永远不会被执行,因此应用程序编译?

public ActionResult MoveOld(string id)
{
    string pagename = String.Empty;

    if(id == "2")
    {
      pagename = WebPage.SingleOrDefault(x => x.ID == 5).URL;
    }

    Response.StatusCode = 301;
    Response.StatusDescription = "301 Moved Permanently";
    Response.RedirectLocation = pagename;
    Response.End();

    return RedirectToAction("Details", new { pageName = pagename });
}
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc asp.net-mvc-routing

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

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

如何在创建帐户时为用户分配角色

我正在使用asp.net内置成员资格和角色提供程序.

在我的应用程序管理员为用户创建帐户.

目前我通过访问asp.net配置网站分配角色.

请告诉我如何在创建用户帐户API中添加额外的选择角色步骤.

谢谢.

asp.net asp.net-mvc

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

Task.Wait in ContinueWhenAll Action

我正在努力将线程包含在我的azure代码中,以便将东西放入队列中.要做到这一点,我使用http://www.microsoft.com/download/en/details.aspx?id=19222作为参考.

我将多条消息排入队列的代码如下所示:

public void AddMessagesAsync(IEnumerable<IQueueMessage> messages, string queue = null, TimeSpan? timeToLive = null)
{
  //check if we need to switch queues
  if (!String.IsNullOrEmpty(queue))
  {
    SetCurrent(queue);
  }

  //setup list of messages to enqueue
  var tasks = new List<Task>();
  Parallel.ForEach(messages, current => {
    if (timeToLive.HasValue)
    {
      //create task with TPL
      var task = Task.Factory.FromAsync(Current.BeginAddMessage, Current.EndAddMessage, Convert(current), timeToLive.Value, tasks); 
      //setup continuation to trigger eventhandler
      tasks.Add(task.ContinueWith((t) => AddMessageCompleted(t)));
    }
    else
    {
      //create task with TPL
      var task = Task.Factory.FromAsync(Current.BeginAddMessage, Current.EndAddMessage, Convert(current), …
Run Code Online (Sandbox Code Playgroud)

c# task azure plinq

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

使用 xsd.exe 工具从具有多个命名空间的 XML 文件生成 XSD 文件

我想做的事:

我正在尝试为现有的 XML 文件生成 XSD 文件。

我正在使用该xsd.exe工具(随 Visual Studio 一起提供)。

XML 文件中的某些元素是命名空间限定的。在某些情况下,本地名称是相同的,如下所示:

<images>
  <icons>
    <icon url="http://www.icons.com/logo.png"/>     
  </icons>
  <foo:icons>
    <foo:foo_icon url="http://www.foo.org/pic.ico"/>
  </foo:icons>
</images>
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

打电话xsd.exe myfile.xml给我一个错误:Cannot add a column named 'icons': a nested table with the same name already belongs to this DataTable.

好的,但这就是命名空间的用途,不是吗?像这样解决歧义。如果没有命名空间,我只会调用元素foo_icons而不是玩弄前缀。

我试过的:

我尝试寻找一种配置方法,xsd.exe以便将名称空间考虑在内,但xsd /?我的 google 查询都没有透露任何答案。该/n[amespace]:参数不允许指定多个命名空间。

我已经阅读了在 XML 模式中使用命名空间,但我觉得自己不太聪明。

我是否必须创建单独的 XSD 文件并将它们嵌入到彼此中?它也不涉及xsd.exe为此目的使用。

我真的不太熟悉XSD,所以我可能误解了整个过程的一些基本概念。如果有人能指出我正确的方向,我将不胜感激。

编辑 1 - 遵循 Marc Gravell 的建议:

我试过了,但我也不得不重命名出现在 …

.net xml xsd xsd.exe .net-3.5

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

保存cookie时会话丢失

成功登录后,我想保存一个包含用户名的cookie.

cookie正确保存并正确加载用户名但丢失会话!

retreive用户名的代码是:

if (Request.Cookies["userName"] != null)
{
  txtEmail.Text = Request.Cookies["username"].Value;
  chkRemember.Checked = true;
}
Run Code Online (Sandbox Code Playgroud)

保存用户名的代码是:

HttpCookie aCookie = new HttpCookie("username");
aCookie.Value = txtEmail.Text;
aCookie.Expires = DateTime.Now.AddYears(5);
Response.Cookies.Add(aCookie);
Run Code Online (Sandbox Code Playgroud)

非常感谢任何帮助,谢谢

c# asp.net cookies session

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

调用泛型扩展方法而不指定任意类型

这是我用于测试Type扩展方法的类的片段:

class Something
{
  [StringLength(100, MinimumLength = 1, ErrorMessage = "Must have between 1 and 100 characters")]
  public string SomePublicString { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我有以下扩展方法:

public static class TypeExtensions
{
  public static TAttributeType GetCustomAttribute<T, TAttributeType, TProperty>(this T value, Expression<Func<T, TProperty>> propertyLambda, bool inherit = false)
  {
    var type = typeof(T);
    var member = (MemberExpression)propertyLambda.Body;
    var propertyInfo = (PropertyInfo)member.Member;
    var customAttributes = propertyInfo.GetCustomAttributes(typeof(TAttributeType), inherit);

    return customAttributes.OfType<TAttributeType>().FirstOrDefault();
  }
}
Run Code Online (Sandbox Code Playgroud)

用于单元测试:

1:  var something = new Something();
2:  var actual = something.GetCustomAttribute<Something, …
Run Code Online (Sandbox Code Playgroud)

c# lambda extension-methods

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

有没有办法为urlCreator.createObjectURL(blob)创建文件名

我不太熟悉Javascript,有没有办法创建自己的文件名?发生的情况是它将打开一个新选项卡,并且文件名采用Guid格式。

if (urlCreator && headers['content-type'] != 'application/octet-stream') {
  // Fallback to $window.location method
  try {
    // Prepare a blob URL
    // Use application/octet-stream when using $window.location to force download
    var objectUrl = urlCreator.createObjectURL(blob);
    $window.open(objectUrl);
    success = true;
  } catch (ex) {
    //console.log("Download link method with $window.location failed with the following exception:");
    //console.log(ex);
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript

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

删除与模式匹配的 Redis 键

我使用 Redis 哈希集以以下格式存储数据:

hset b1.b2.b3 name test
Run Code Online (Sandbox Code Playgroud)

现在我想删除这个键,所以我使用以下格式:

del b1.b2.*
Run Code Online (Sandbox Code Playgroud)

但它不起作用,那么我如何使用模式删除 Redis 键呢?

redis redis-cli redisclient

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