11

最初は、ある種のenumとして定義されていると思いました。そうではありません。

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();
}

また、ILSpy は実装を次のように示しています。

// System.Web.Mvc.UrlParameter
/// <summary>Contains the read-only value for the optional parameter.</summary>
public static readonly UrlParameter Optional = new UrlParameter();

では、MVC は、次のコードを見たときに、オプションのパラメーターをディクショナリに入れないようにどのように判断するのでしょうか? 結局のところ、以下のコードは UrlParameter の新しいインスタンスを ID に割り当てるだけです。

defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
4

1 に答える 1