views:

66

answers:

1

I'm just starting out with C# and ASP.NET and have the following questions. I am working with code adapted from a couple different tutorials playing with Northwind and have gotten this far. The list of acceptable categories is currently hard coded in a string but I would like to look up the CategoryName in the database to verify that it exists.

Obviously the purpose of this is to ensure that users don't just type:
www.domain.com/Categories/AnyUrlWillWork and return a valid page.

Also does anyone have an tips of how they are dealing with capitalization issues since the routing is case sensitive? For example Categories/beverages should forward to Categories/Beverages ?

Thanks in advance for any assistance, and glad to be joining Stack Overflow.

//Complex contraint class
public class EchoConstraint : IRouteConstraint
{
    public readonly string[] ValidMessages = { "Beverages", "Produce", "Confections", "Seafood" };

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        string message = values["CategoryName"] as string;
        return ValidMessages.Contains(message);
    }
}


//Routes
RouteTable.Routes.MapPageRoute(
                        "Category Route",                       // Route name
                        "Categories/{CategoryName}",            // Url pattern
                        "~/ShowProductsInCategory.aspx",        // Physical file
                        true,
                        new RouteValueDictionary            
                            {{"CategoryName", "Beverages"}},         //Sets default value if none is provided in URL
                        new RouteValueDictionary 
                            {{"CategoryName", new EchoConstraint()}}
                           );
+1  A: 

Is this MVC? If so, why not route to a function, which will check the category name against your data store and redirect to an error page if such category doesn't exist?

public ActionResult Index(string catName)
{
    if (string.IsNullOrEmpty(catName) || !MyCategoriesDataStore.Exists(catName))
        return RedirectToAction("CategoryError");

    // Load category data to be used from View

    return View();
}

A WebForms solution would be:

    public class EchoConstraint : IRouteConstraint
    {
        private IRepository rep;
        public EchoConstraint(IRepository r) { rep = r; }

        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            return rep.GetCategory(message) == 0;
        }
    }
.
.
.
                        new RouteValueDictionary 
                            {{"CategoryName", new EchoConstraint(myDataAccessRepo)}}
                           );

Where you pass an object of class implementing IRepository with your data access logic (using NHibernate, EntityFramework or your own DAL implementation). You need to return a bool value, and this is what I did.

synhershko
Thanks for the response. I have not dived into MVC yet unfortunately. That's my next step after catching up...I haven't played with ASP.NET since the 1.1 days so this is all new. I am working off of this tutorial BTW:http://www.4guysfromrolla.com/articles/050510-1.aspx
Mike
I think you should jump right into MVC. Routing in WebForms isn't as natural as it is there, and IMHO WebForms will die a slow death now that MVC is around.
synhershko
I was considering it but I need to catch up on a lot of the basics and the training/classes I have are not covering MVC. Once I get through them I'll be jumping on the wagon with ya.
Mike
I edited my answer to provide you with a way to do this with WebForms too, see if it helps
synhershko
Thanks for adding. It is a little over my head, but that is what I get for jumping into this. So far I have been reading this tutorial: http://www.asp.net/data-access/tutorials/creating-a-data-access-layer-cs and have created a DAL that returns the Category names ( categoriesAdapter.GetCategories(); ) but am not sure how to proceed and use it in this class. I can bind to a control on a page however.
Mike
Just add another function to your DAL which will pull a category out of storage by its (unique) alias, and make sure an object is returned (if it isn't -- the category doesn't exist). In the code above I called it GetCategory.
synhershko
Still poorly hacking away at this. I'm stuck on creating a function that will work. Any code hints on writing that? Also do I need a specific namespace for IRepository?
Mike
No, that is the easiest function, really. Just make it use a correct SELECT statement and you are done. No specific namespace, but if you use another you'll need to add a "using" statement at the top of the file (the IDE will cry about this one, so don't worry).
synhershko