1

是否可以将参数从“url”部分传递给 MapRoute 的“默认”部分中的方法?

routes.MapRoute(
               name: "Language",
               url: "{language}/{controller}/{action}/{id}-{description}",
               defaults: new { Controller = "Home", action = "Index", id = UrlParameter.Optional, language = UrlParameter.Optional, description = GetDescription(id) }
           );

我可以在没有任何参数的情况下调用 GetDescription(),但我不知道如何从 url 传递参数,即。获取描述(ID)?

4

1 回答 1

1

我认为您误解了路由定义的概念。您不能将动态行为添加到死记硬背的注册过程中,因为它只在应用程序启动事件中执行一次。当路由引擎在提供的 URL 中找不到合适的参数时,使用 defaults 参数。你想做什么是可能的 url 生成级别:

@Html.ActionLink("Home", "Index", new { language = "en", id=5, description = "test" })
于 2014-11-26T08:30:28.763 回答