Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MapPageRoute-can';在中间发送空字符串?_C#_Asp.net_Url Routing_Invalidoperationexception_Asp.net Routing - Fatal编程技术网

C# MapPageRoute-can';在中间发送空字符串?

C# MapPageRoute-can';在中间发送空字符串?,c#,asp.net,url-routing,invalidoperationexception,asp.net-routing,C#,Asp.net,Url Routing,Invalidoperationexception,Asp.net Routing,在我维护的一个项目的Global.asax.cs中,有一个方法如下所示 private static void MapRouteWithName(string name, string url, string physicalPath, bool? checkPhysicalUrlAccess = null, RouteValueDictionary defaults = null) { Route route; if ((checkPhysicalUrlAccess != null)

在我维护的一个项目的Global.asax.cs中,有一个方法如下所示

private static void MapRouteWithName(string name, string url, string physicalPath, bool? checkPhysicalUrlAccess = null, RouteValueDictionary defaults = null)
{
  Route route;
  if ((checkPhysicalUrlAccess != null) && (defaults != null))
  {
    route = System.Web.Routing.RouteTable.Routes.MapPageRoute(name, url, physicalPath, checkPhysicalUrlAccess.Value, defaults);
  }
  else
  {
    route = System.Web.Routing.RouteTable.Routes.MapPageRoute(name, url, physicalPath);
  }
  route.DataTokens = new RouteValueDictionary();
  route.DataTokens.Add("RouteName", name);
}
然后在Global.asax.cs中使用它设置如下路由:

MapRouteWithName("Change User", "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}", "~/User/Change.aspx", true,
  new RouteValueDictionary { { "id", "0" }, { "organizationid", "0" }, { "username", "" }, { "logonaccount", "" }, { "phone", "" } });
Response.RedirectToRoute("Change User", new
{
  id = userID,
  organizationid = selectedOrganizationID,
  username = userName,
  logonaccount = logonAccount,
  phone = phone
});
routes.MapPageRoute(
    routeName: "ChangeUser1",
    routeUrl: "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}",
    physicalFile: "~/User/Change.aspx",
    checkPhysicalUrlAccess: true,
    defaults: new RouteValueDictionary(new {
        phone = UrlParameter.Optional,
    })
);

routes.MapPageRoute(
    routeName: "ChangeUser2",
    routeUrl: "User/Change/{id}/{organizationid}/{username}",
    physicalFile: "~/User/Change.aspx",
    checkPhysicalUrlAccess: true,
    defaults: new RouteValueDictionary(new
    {
        username = UrlParameter.Optional
    })
);
然后在页面后面的代码中可以找到如下内容:

MapRouteWithName("Change User", "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}", "~/User/Change.aspx", true,
  new RouteValueDictionary { { "id", "0" }, { "organizationid", "0" }, { "username", "" }, { "logonaccount", "" }, { "phone", "" } });
Response.RedirectToRoute("Change User", new
{
  id = userID,
  organizationid = selectedOrganizationID,
  username = userName,
  logonaccount = logonAccount,
  phone = phone
});
routes.MapPageRoute(
    routeName: "ChangeUser1",
    routeUrl: "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}",
    physicalFile: "~/User/Change.aspx",
    checkPhysicalUrlAccess: true,
    defaults: new RouteValueDictionary(new {
        phone = UrlParameter.Optional,
    })
);

routes.MapPageRoute(
    routeName: "ChangeUser2",
    routeUrl: "User/Change/{id}/{organizationid}/{username}",
    physicalFile: "~/User/Change.aspx",
    checkPhysicalUrlAccess: true,
    defaults: new RouteValueDictionary(new
    {
        username = UrlParameter.Optional
    })
);
现在,这在大多数情况下都能很好地工作,但在这个特定的示例中,我不知道变量userName、logonAccount和phone是否确实包含某些内容,或者是空的。例如,当userName为空(“”)且logonAccount具有值(“abcde”)时,将引发异常:

System.InvalidOperationException: No matching route found for RedirectToRoute.

当在具有值的变量之间有一个空字符串的变量时,似乎会引发异常。(这有意义吗?)对此我能做些什么?

这里的问题是重定向与路由不匹配,因为您错误地声明了可选值。使其成为可选的唯一方法是采用如下配置:

MapRouteWithName("Change User", "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}", "~/User/Change.aspx", true,
  new RouteValueDictionary { { "id", "0" }, { "organizationid", "0" }, { "username", "" }, { "logonaccount", "" }, { "phone", "" } });
Response.RedirectToRoute("Change User", new
{
  id = userID,
  organizationid = selectedOrganizationID,
  username = userName,
  logonaccount = logonAccount,
  phone = phone
});
routes.MapPageRoute(
    routeName: "ChangeUser1",
    routeUrl: "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}",
    physicalFile: "~/User/Change.aspx",
    checkPhysicalUrlAccess: true,
    defaults: new RouteValueDictionary(new {
        phone = UrlParameter.Optional,
    })
);

routes.MapPageRoute(
    routeName: "ChangeUser2",
    routeUrl: "User/Change/{id}/{organizationid}/{username}",
    physicalFile: "~/User/Change.aspx",
    checkPhysicalUrlAccess: true,
    defaults: new RouteValueDictionary(new
    {
        username = UrlParameter.Optional
    })
);
当最后3个参数中的任何一个不在重定向范围内时,这将始终有效但是,有一个问题-当您省略一个参数时,右侧的所有参数也将为空。这就是内置路由的工作方式。参数只有在其右侧没有值时才可以是可选的

如果希望所有3个参数都是可选的,并且不必担心参数传递的顺序或组合,则有2个选项:

  • 使用查询字符串参数
  • 使用约定建立一系列路线,如中所示

  • 在处理具有大量可选参数的搜索表单时,查询字符串参数是一个简单得多的选项,因为它们可以以任意顺序和任意组合提供,而无需做任何额外的工作。它们自然适合这种情况。

    用户需要有名字吗?如果是,这种情况意味着什么?变量userName、logonAccount和phone实际上是搜索词。这里发生的事情是,显示了现有用户的列表,并单击其中一个用户以进行更改。这三个变量随后不会在更改页面的表单中使用,而是为了能够返回到列表页面,就像在单击用户之前看到的那样。如果有人搜索了每个包含“abcde”的用户名,那么应该恢复特定的列表。