在我的代码中,我在global.asax中注册了路由,如下所示:
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute(
"product",
"product/{id}",
"~/index.aspx"
);
Run Code Online (Sandbox Code Playgroud)
然后在我的index.aspx页面中,我想测试{id}值是否为整数:
if (Page.RouteData.Values["id"] as int? != null)
//always evaluate as null even id is an integer in the URL, ex: /product/1
Run Code Online (Sandbox Code Playgroud)
我理解我可以使用其他一些方法(例如int.TryParse或this)来实现相同的目标,但在这种情况下会使用"as"有意义吗?