2012-11-16 50 views
1

我想创建一个使用params作为URL一部分的路由。在路由中使用C#关键字的Asp.Net MVC路由

我遇到的问题是当我创建默认值时,我不能使用params作为其保留的c#单词。

想知道是否有解决方法。 (请参阅使用PARAMS的路线,并作为默认使用时,我得到一个编译错误:表达式预期)

下面是示例代码:

routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{db}/{proc}/{params}", 
      defaults: new { controller = "Data", action = "Index", db = UrlParameter.Optional, proc = UrlParameter.Optional, params = UrlParameter.Optional} 
     ); 

回答

4

您可以通过预先@符号,它们使用C#关键字,正如MSDN所建议的那样。 在你的情况下,使用@params而不是params

To quote MSDN

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier but if is not because if is a keyword.

是否有你需要这个变量被称为params什么特别的原因?

+0

我发现添加@params实际上可行! –

+0

routes.MapRoute( name:“Default”, url:“{controller}/{action}/{db}/{proc}/{params}”, 默认值:new {controller =“Data”,action = “Index”,db = UrlParameter.Optional,proc = UrlParameter.Optional,@params = UrlParameter.Optional} ); –

+0

奇怪,我试过了,失败了。我一定是犯了一个错字。感谢您的回应。我相应地更新了我的答案。 –