MapPageRoute - 不能在中间发送空字符串? [英] MapPageRoute - can't send empty strings in the middle?

查看:47
本文介绍了MapPageRoute - 不能在中间发送空字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Global.asax.cs in a project that I'm maintaining there is a method that looks like this

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 中使用它来设置这样的路由:

It's then used in Global.asax.cs to set up routes like this:

MapRouteWithName("Change User", "User/Change/{id}/{organizationid}/{username}/{logonaccount}/{phone}", "~/User/Change.aspx", true,
  new RouteValueDictionary { { "id", "0" }, { "organizationid", "0" }, { "username", "" }, { "logonaccount", "" }, { "phone", "" } });

然后在页面后面的代码中可以找到这样的东西:

And then in the code behind a page one can find things like this:

Response.RedirectToRoute("Change User", new
{
  id = userID,
  organizationid = selectedOrganizationID,
  username = userName,
  logonaccount = logonAccount,
  phone = phone
});

现在这在大多数情况下都能很好地工作,但在这个特定示例中,我不知道变量 userName、logonAccount 和 phone 是否实际包含某些内容或为空.例如,当 userName 为空 ("") 并且 logonAccount 具有值 ("abcde") 时,将引发异常:

Now this works nicely in most cases but in this particular example I don't know if the variables userName, logonAccount and phone actually contains something or is empty. When for instance userName is empty ("") and logonAccount has a value ("abcde") an exception is thrown:

System.InvalidOperationException: No matching route found for RedirectToRoute.

当在具有值的变量之间存在一个带有空字符串的变量时,似乎会引发异常.(这有意义吗?)我该怎么办?

It seems like the exception is thrown when there is a variable with an empty string in between variables that has values. (Did this make sense?) What can I do about this?

推荐答案

这里的问题是重定向与路由不匹配,因为您错误地声明了可选值.使它们成为可选的唯一方法是具有这样的配置:

The issue here is that the redirect doesn't match the route because you are declaring optional values incorrectly. The only way to make them optional would be to have a configuration like this:

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 个参数中的任何一个被排除在重定向之外时,这将始终有效.但是,有一个问题 - 当您省略一个参数时,右侧的所有参数也将为空.这就是内置路由的工作方式.一个参数只有在它右边没有值时才可以是可选的.

This will always work when any of the last 3 parameters are left off of the redirect. However, there's a catch - when you leave out a parameter, all of the parameters to the right will also be empty. That is the way the built-in routing works. A parameter can only be optional if there is no value to the right of it.

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

If you want to have all 3 parameters optional and not have to worry about what order or combination the parameters are passed, you have 2 options:

  1. 使用查询字符串参数
  2. 使用约定来构建一系列路由,如本示例

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

Query string parameters are the far simpler option when dealing with search forms with lots of optional parameters because they can be supplied in any order and any combination without having to do anything extra. They are a natural fit for this scenario.

这篇关于MapPageRoute - 不能在中间发送空字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆