检查Page.RouteData.Values是否有值

Rav*_*Ram 2 c# asp.net routes

我跑步时遇到错误

string quote = Page.RouteData.Values["quote"].ToString() ?? string.Empty;
Run Code Online (Sandbox Code Playgroud)

错误:对象引用未设置为对象的实例.

我知道ToString导致错误,因为Page.RouteData.Values ["quote"]为空/ null.

在执行ToString之前,如何检查Page.RouteData.Values ["quote"]是否有值?

pop*_*tea 8

怎么样:

if (Page.RouteData.Values["quote"] != null) {
    string quote = Page.RouteData.Values["quote"].ToString() ?? string.Empty;
}
Run Code Online (Sandbox Code Playgroud)

要么

string quote = ((Page.RouteData.Values["quote"] != null) ? Page.RouteData.Values["quote"].ToString() : string.Empty);
Run Code Online (Sandbox Code Playgroud)