2012-11-16 75 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

奇怪,我試過了,失敗了。我一定是犯了一個錯字。感謝您的迴應。我相應地更新了我的答案。 –