2010-06-14 20 views
0

我有這些編碼器塊天之一。我應該知道這一點,但我會尋求一些幫助。我有兩個途徑:可空類型的查詢字符串路由

/Login 
/Login?wa=wsignin1.0&wtrealm=http://localhost/MyApp 

訪問的第一個操作方法與HTTP GET返回登錄頁面,在這裏,作爲第二個做一些聯合身份驗證的東西。我定義了兩種控制器方法:

public ActionResult Index(); 
public ActionResult Index(string wa); 

路由當然不會這樣,因爲可空類型使它不明確。我如何設置一個約束來表示只有在路徑數據中存在該值時才執行第二個方法?

編輯:我暫時解決了與操作方法選擇器的問題。這是最好的方法嗎?

public class QueryStringAttribute : ActionMethodSelectorAttribute 
{ 
    public ICollection<string> Keys { get; private set; } 

    public QueryStringAttribute(params string[] keys) 
    { 
     this.Keys = new ReadOnlyCollection<string>(keys); 
    } 

    public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo) 
    { 
     var requestKeys = controllerContext.HttpContext.Request.QueryString.AllKeys; 
     var result = Keys.Except(requestKeys, StringComparer.OrdinalIgnoreCase).Count() == 0; 
     return result; 
    } 
} 

回答

0

我在過去遇到過這個問題很多次,我認爲這是一個經典的路由問題。我所做的是這樣的:

在控制器中創建自己的行爲:

public ActionResult Index(); 
public ActionResult IndexForWa(string wa); 

你需要在你的路由定義做任何映射

routes.MapRoute(
    "index_route", 
    "Login" 
    new {controller="Login", action="Index"} 
); //This is not even necessary but its here to demo purposes 

routes.MapRoute(
    "index_for_wa_route", 
    "Login/wa/{wa}", 
    new {controller="Login", action="Index", wa = {wa)} 
);