asp.net mvc - Good use of Attribute to Determine correct method to call -


i have 2 action methods called using same action name, however, depending on actual parameter type depends method should called. causes ambiguity. created attribute determines if parameter guid , appropriate method.

[requiredguidparameter(parametername = "title")] [actionname("title")] public actionresult item_byid(guid id) { ... }  [actionname("title")] public actionresult item_byname(string id) { ... } 

the attribute looks this:

    public string parametername = string.empty;      public override bool isvalidforrequest(controllercontext controllercontext, system.reflection.methodinfo methodinfo)     {         object parameter = null;         try         {             parameter = controllercontext.routedata.getrequiredstring(parametername) object;             if (parameter != null)             {                 guid guid;                 return guid.tryparse((string)parameter, out guid);             }         }         catch { }          parameter = controllercontext.requestcontext.httpcontext.request[parametername] object;         if (parameter != null)         {                 guid guid;                 return guid.tryparse((string)parameter, out guid);         }          return false;     } 

the ultimate goal being if parameter guid run method, otherwise move on, in case finds next one. there better way not invlove creating additional route? or perhaps better way around?

why not like:

public actionresult item_search(string id, guid guid) {     if( string.isnullorwhitespace(id)           searchbyid();      if( guid != new guid() )          searchbyguid() } 

non warranty'd psuedo code /\


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -