Asp.net mvc 2 具有可空类型的查询字符串路由

Asp.net mvc 2 具有可空类型的查询字符串路由,asp.net-mvc-2,asp.net-mvc-routing,Asp.net Mvc 2,Asp.net Mvc Routing,我正在经历一个编码员的封锁日。我应该知道这一点,但我会寻求一些帮助。我有两条路线: /Login /Login?wa=wsignin1.0&wtrealm=http://localhost/MyApp 使用HTTPGET访问第一个操作方法将返回登录页面,其中第二个执行一些联邦身份验证操作。我定义了两种控制器方法: public ActionResult Index(); public ActionResult Index(string wa); 路由当然不喜欢这样,因为可为null的

我正在经历一个编码员的封锁日。我应该知道这一点,但我会寻求一些帮助。我有两条路线:

/Login
/Login?wa=wsignin1.0&wtrealm=http://localhost/MyApp
使用HTTPGET访问第一个操作方法将返回登录页面,其中第二个执行一些联邦身份验证操作。我定义了两种控制器方法:

public ActionResult Index();
public ActionResult Index(string wa);
路由当然不喜欢这样,因为可为null的类型使其不明确。如果路由数据中存在该值,如何对其进行约束,使其仅执行第二个方法

编辑:我已经用一个动作方法选择器暂时解决了这个问题。这是最好的方法吗

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;
    }
}
公共类QueryStringAttribute:ActionMethodSelectorAttribute
{
公共ICollection密钥{get;private set;}
公共QueryStringAttribute(参数字符串[]键)
{
this.Keys=新的只读集合(Keys);
}
公共覆盖布尔值是有效的请求(ControllerContext ControllerContext,System.Reflection.MethodInfo)
{
var requestKeys=controllerContext.HttpContext.Request.QueryString.AllKeys;
var result=Keys.Except(requestKeys,StringComparer.OrdinalIgnoreCase).Count()=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)}
);