1

次の URL をコントローラーにルーティングする必要があります。

  1. /de/flight-ロンドン
  2. /de/flight-サンクトペテルブルク

Global.asax で定義する正しい URL-Mapping-String は何ですか?

私が試してみました:

  1. "{countrycode}/{keyword}-{destination}" -> 1 には適していますが、2 には適していません
  2. "{countrycode}/{keyword}-{*destination}" -> Exception !

誰かが私を助けてくれることを願っています。

4

2 に答える 2

1

/de/flight-st-petersburg既知のバグのため動作しません。次の 2 つのオプションがあります。

  1. キーワードと目的地をスラッシュで区切ります。{countrycode}/{keyword}/{destination}
  2. 次のように、モデル バインダーを使用します。

.

class CustomIdentifier {

   public const string Pattern = @"(.+?)-(.+)";
   static readonly Regex Regex = new Regex(Pattern);

   public string Keyword { get; private set; }
   public string Value { get; private set; }

   public static CustomIdentifier Parse(string identifier) {

      if (identifier == null) throw new ArgumentNullException("identifier");

      Match match = Regex.Match(identifier);

      if (!match.Success)
         throw new ArgumentException("identifier is invalid.", "identifier");

      return new CustomIdentifier {
         Keyword = match.Groups[1].Value,
         Value = match.Groups[2].Value
      };
   }
}

class CustomIdentifierModelBinder : IModelBinder {

   public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
      return CustomIdentifier.Parse(
         (string)bindingContext.ValueProvider.GetValue(bindingContext.ModelName).RawValue
      );
   }
}

そして、それを Application_Start に登録します。

void RegisterModelBinders(ModelBinderDictionary binders) {
   binders.Add(typeof(CustomIdentifier), new CustomIdentifierModelBinder());
}

次のルートを使用します。

routes.MapRoute(null, "{countryCode}/{id}",
   new { },
   new { id = CustomIdentifier.Pattern });

そしてあなたの行動:

public ActionResult Flight(CustomIdentifier id) {

}
于 2013-07-02T16:23:54.307 に答える
0

ルート部分はスラッシュで区切られているため、次のように使用する必要があります。

{countrycode}/{keyword}/{*destination}
于 2013-07-02T15:52:41.240 に答える