2010-08-29 21 views
7

在我的全球ASAX文件,我想一個路線圖,如本:如何使用「?」路由查詢字符串以及如何處理它

http://domain.com/add/link?url=http%3A%2F%2Fgoogle.com 

,然後用我的LinkController用行動呼籲加抓住它。

我會這樣做嗎?

global.asax->

routes.MapRoute(
    "AddLink", 
    "Add/Link?{url}", 
    new { controller = "Link", action = "Add" } 
); 

LinkController->

public string Add(string url) 
{ 
    return url; // just want to output it to the webpage for testing 
} 

??這似乎並不奏效。我究竟做錯了什麼?謝謝!

回答

15

ASP.Net MVC會自動綁定來自查詢字符串的參數;你不需要把它放在路線上。

您的路線可以簡單地

routes.MapRoute(
    "AddLink", 
    "Add/Link", 
    new { controller = "Link", action = "Add" } 
); 
+0

非常感謝!那樣做了。 – 2010-08-30 04:38:37

0

顯示MVC源代碼ValueProviderFactories。

namespace System.Web.Mvc { 
    using System; 

    public static class ValueProviderFactories { 

     private static readonly ValueProviderFactoryCollection _factories = new ValueProviderFactoryCollection() { 
      new FormValueProviderFactory(), 
      new RouteDataValueProviderFactory(), 
      new QueryStringValueProviderFactory(), 
      new HttpFileCollectionValueProviderFactory() 
     }; 

     public static ValueProviderFactoryCollection Factories { 
      get { 
       return _factories; 
      } 
     } 

    } 
} 
+1

恩,現在做什麼? ;) – 2010-09-02 14:11:14

+0

綁定IModelBinder的動作參數。 DefaultModelBinder從HttpRequest中提取值使用ValueProviderFactories。所以動作參數自動綁定Form/Route/QueryString/UplodFile。 – takepara 2010-09-03 01:35:12

相關問題