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;
}
}