mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Routes with defaulted querystring constraints do not match when querystring parameter is missing #270

Open brantb opened 11 years ago

brantb commented 11 years ago

Given an action method with a route defined as below, the URL /Index does not match any route.

public class HomeController : Controller
{
    [GET("Index?{page=1}")]
    public ActionResult Index(int page)
    {
        return View("Index", page);
    }
}

I expected that a URL like /Index would match this route and default the page routedata value to 1, but it does not.

When I actually specify a page querystring value (like /Index?page=4) it works as expected.

mccalltd commented 11 years ago

I just looked at the specs and I don't have this case covered. :(

The fastest way for this to get fixed would be if you submitted a PR. I've been super busy with other things so it'll be a little bit before I can get to this and other issues, unfortunately.

In any case, thanks for reporting the issue!

brantb commented 11 years ago

Thanks for the fast response! I'll take a look at the code and see if I can put together a PR.

As a temporary workaround, it seems to behave as expected if I change the route declaration and method signature to the following:

[GET("Index?{page=1?}")]
public ActionResult Index(int page = 1)
{
    return View("Index", page);
}
mccalltd commented 11 years ago

Yeah. I suspected that adding a default value would work as a workaround. But it's gross to specify the default value twice!

And FYI: that ? at the end of page=1? should have no effect. But you can leave it in if it strikes you as pretty. :)

mccalltd commented 11 years ago

Actually, for now I'd just do this for a workaround:

[GET("Index?{page?}")]
public ActionResult Index(int page = 1)
brantb commented 11 years ago

And FYI: that ? at the end of page=1? should have no effect. But you can leave it in if it strikes you as pretty. :)

That's what I thought too, but when I take it out the route no longer matches. Weird, huh?

Your second suggestion works, though.