起初,我认为它被定义为某种枚举.但事实并非如此.
UrlParameter定义如下:
// Summary:
// Represents an optional parameter that is used by the System.Web.Mvc.MvcHandler
// class during routing.
public sealed class UrlParameter
{
// Summary:
// Contains the read-only value for the optional parameter.
public static readonly UrlParameter Optional;
// Summary:
// Returns an empty string. This method supports the ASP.NET MVC infrastructure
// and is not intended to be used directly from your code.
//
// Returns:
// An empty string.
public override string ToString();
}
Run Code Online (Sandbox Code Playgroud)
ILSpy将实现显示为:
// System.Web.Mvc.UrlParameter
/// <summary>Contains the read-only value for the optional parameter.</summary>
public static readonly UrlParameter Optional = new UrlParameter();
Run Code Online (Sandbox Code Playgroud)
那么MVC如何知道在看到以下代码时不将可选参数放入字典中?毕竟,下面的代码只是为id分配了一个新的UrlParameter实例.
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
Run Code Online (Sandbox Code Playgroud)
看看更广泛的背景.
完整的源代码UrlParameter是
public sealed class UrlParameter {
[SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "This type is immutable.")]
public static readonly UrlParameter Optional = new UrlParameter();
// singleton constructor
private UrlParameter() { }
public override string ToString() {
return String.Empty;
}
}
Run Code Online (Sandbox Code Playgroud)
UrlParameter.Optional是唯一可能的实例UrlParameter.
换句话说,整个UrlParameter类仅作为可选参数的占位符存在.
| 归档时间: |
|
| 查看次数: |
6075 次 |
| 最近记录: |