3

可能重复:
C# 中的命名字符串格式

有没有人找到在 String.Format 命令中使用命名标识符的方法?拥有这种功能对于像我们这样记忆力衰退的“老”人来说会很有帮助。我的意思是,既然大括号在这个命令中是一个保留字符,那么其中的文本真的很重要吗?

MVC 在定义路由时是否遵循大致相同的语法?

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

所以不要使用:

String.Format("http://foobar.com/calendar/{0}/{1}", selectedYear, selectedMonth);

是不是会跟着看到:

String.Format("http://foobar.com/calendar/{year}/{month}", selectedYear, selectedMonth);

甚至:

String.Format("http://foobar.com/calendar/{year}/{month}", new {selectedYear, selectedMonth});

第二个语句显然没有编译,但我想知道是否有人找到了实现该功能的方法。

4

0 回答 0