Url-router API design question
I'm creating a Url router for a web framework. Trying to make as friendly to use as possible. The API looks something like:
Route[] Routes = {
new Route(@"/user:(?<id>\d+)", "UserController.View")
};
Where it would route any Url that matches that regex to that method, which might look something like this:
public class UserController : Controller
{
public void View(int id)
{
开发者_运维知识库 // code here
}
}
It handles the type-casting automatically.
There is a correlation between (?<id>...)
and the arguments that the method takes. The View()
function must accept all the arguments provided by the name captures. However, it can accept more arguments as long as they're optional. For example, that regex would also match the function View(int id, int extraArg=2)
, since we don't really need the extraArg
to call the function.
The question is, should that route also match the function View()
-- with no arguments? The id
capture can easily be discarded and we can still call the function just fine. Or should it throw an exception (as it does presently)?
Take the best programming approach: you don't want the user to encounter any errors. Seeing that the user could accidentally/deliberately put extra parameters in at any stage, I'd just take the necessary ones, and ignore the rest.
精彩评论