2013-07-02 50 views
1

正确的,我需要的路线:MVC4找不到以下网址到控制器路由

  1. /德/飞行伦敦
  2. /德/飞行圣彼得堡

什么是在Global.asax中定义的正确的URL-Mapping-String?

我曾尝试:

  1. “{COUNTRYCODE}/{关键词} - {目的地}” - >好1而不是2
  2. “{COUNTRYCODE}/{关键词} - {*目的地}“ - >例外!

我希望有人能帮助我。

回答

0

路线部分由斜线分开,所以你就需要使用,我相信:

{countrycode}/{keyword}/{*destination} 
+0

嗨麦克,THX为你解答但我们的SEO专家喜欢这个网址schem a:/ de/flight-st-petersburg。 你的路线只能匹配/ de/flight/st-peterburg 8( –

+0

)正如哈克的帖子中所提到的,马克斯托罗提到你可以做一些事情,比如他推荐的路线约束。保持我放的路线,然后有路线约束条件,要求目的地以短划线(或关键字)开头 –

+0

嗨迈克,抱歉,但我不明白你的想法,你能更清楚一点吗? –

1

/de/flight-st-petersburg不会因为known bug的工作。你有两个选择:

  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) { 

}