1

Lets say I have a route; www.kunduz.com/stuff/something

where "something" is being checked against a route constraint;

 public class AnalysisTypePathRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       //check if "something" is really something
    }
}

and lets say stuff has a structure like this

public class Stuff
{
     public int Id{get;set;}
     public string name {get;set;}
     //some other properties
}

Consider that object Stuff is not just an entry in DB, but more like a type. Like, if this was an e-commerce site, it could be "cars" or "furniture".

So what I'm doing is, I'm checking if "something" is really a valid "Stuff" on my route constraint. Then in my controller;

Public class SomeController
{
    public GetStuff(string stuffName)
    {
        //get stuff by its name and get its Id
        //use that Id to do something else
    }
}

Now, what I can also do is this

 public class AnalysisTypePathRouteConstraint : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
       //check if "something" is really something
       values.Add("stuffId",stuff.Id");
    }
}

and get that Id I've added as a parameter on my controller action

    public GetStuff(int stuffId)
    {
        //get stuff by its name and get its Id
        //use that Id to do something else
    }

This would presumably increase performance and it makes more sense to me that I should avoid gettting stuff Id twice.

What my question is, is this a good practice? Since my URL doesnt contain sutffId, my controller action may be confusing for future developers looking at this code.

I would really appreciate some insight into this isssue.

Thank you.

Rudithus
  • 183
  • 13
  • 1
    Don't rely on some `RouteConstraint` to modify the values. It's not what it was meant for. But, why can't you just check the `stuffName` parameter in your action and act accordingly? – haim770 Jan 02 '17 at 08:43
  • Because the customer doesn't want that route to exist if the "stuff" doesn't exist. Consider it as a category in an e-commerce site. If they don't sell furniture, www.buystuff/furnitare url shouldn't exist. – Rudithus Jan 02 '17 at 08:45
  • 2
    In HTTP terms, the appropriate response for those cases is simply a `404 Not Found`. There's nothing wrong with validating input in the controller/action and `return HttpNotFound()` for invalid values. And that's exactly what you'll get when your `RouteConstraint.Match()` would return `false` and no other route will match the Url. – haim770 Jan 02 '17 at 08:48
  • What if there are 2 routes with the same structure existing in the site? www.buystuff/stringparameter could be leading to a category location, if "stringparameter" is a category, and if not, it would lead to a product location or something else. Would this be just a bad design or is there a solution for this? – Rudithus Jan 02 '17 at 08:53
  • 2
    For those cases, an action in some controller would have to get the `stringparameter` and determine what does `stringparameter` stands for (category or product) then `return Product(param)` / `return Category(param)`. Yet, a simple route that will match `/category/{stringparameter}` to `Category` and `/product/{stringparameter}` to `Product` might be cleaner. – haim770 Jan 02 '17 at 09:01
  • That seems to be the routeconstraint's job to me. Could you give me an appropriate use case for the routeconstriant? – Rudithus Jan 02 '17 at 09:02
  • 2
    To me, `RouteConstraint` is applicable only when it can return `true/false` based on `HttpContext` only. For example, extracting values from the querystring, checking for some custom header etc. Anything that involves actual business-logic doesn't belong there (and determining whether `someString` represents a category or a product sounds like a business-logic to me). – haim770 Jan 02 '17 at 09:06
  • You can think of it as a more fine-grained method for route matching (on top of basic pattern matching and regular expressions). – haim770 Jan 02 '17 at 09:09

0 Answers0