views:

220

answers:

1

I'm writing an helper that basically receives many RouteValueDictionary and returns many links; I'd like to skip links to the current route values (example: in /Products/Details/3 there should not be a link to /Products/Details/3), ignoring other URL params if any.

What I can think of is to check if:

  • destination action and controller name are null or equal to the current ones;

  • each other destination route value is defined and equal to the current ones and vice versa.

Is there a better way?

EDIT:

Some context: I have many models, many controllers, many actions. I don't like the idea of writing the same stuff over and over again everywhere, so I decided to code a general solution: permission aware icon links (I asked for hints here).

I now have a PermissionAwareIconAction class, which has an Icon, a HasPermission() method, a delegate to obtain the URL and few other stuff. Inside Global.asax.cs I call few methods to fill an ActionTable, that contains all this stuff, with code like this:

CrudAction.CreateCrudActions<Vendors>(Permissions.CreateVendors, 
                                      Permissions.EditVendors, 
                                      Permissions.DetailsVendors, 
                                      Permissions.DeleteVendors);

and, of course, code to add other specific actions.

I also decorate my controllers actions with:

[Action(typeof(Vendors), "Create")]
public ActionResult Create()
{
  ...
}

Inside views, I do:

<%= Html.IconActionLinks<Vendors>() %>

In views not related to a single object, like Index, and

<%= Html.IconActionLinks<Vendors>(Model) %>

In views related to objects, like Details.

This way, Permissions are defined in just one class; also, code to show or not to show a link to Edit based on permissions is the same code to allow or not allow user to see the Edit action.

I'm almost done (I plan to post a reply to my previous question, resuming what I've done, when I'm done).

Now I have still to do this little piece, avoid Details icon in Details view, Index icon in Index view and so on.

Forgive me for the long post.

A: 

Does your RouteValueDictionary contain the keys for your controller and action as well? Maybe post a bit of code for us to see? I would try something like

var currentPath = Request.PathAndQuery;

loop through each routevalue dictionary { var someString; for each value in dictionary { append to some string }

then check that string against currentPath
if true continue else create the link 
(not too efficient though)

}

The_Butcher
now that i see your comment i agree with Sosh why would you want to do this? because if you are on the details view and you want actions to edit and delete just code that into the details view. Are you using a partial view for all your actions? In that case you could make a view model with 3 bools InDetails, InEdit,InDelete and depending on which view you are on you will hide that link.
The_Butcher
actually yes, the `RouteValueDictionary` contains `action` and `controller` keys, as I create them that way; I'm just trying to write robust code.
giorgian
what are the views you are working with?
The_Butcher
I updated my question explaining what I'm trying to do.
giorgian
so in your details view for the vendor do you still you would just like edit and delete, not details right? if so then i would just change <%= Html.IconActionLinks<Vendors>() %> TO <%= Html.IconActionLinks<Vendors>("LINKTOEXCLUDEHERE") %> and in the IconActionLinks method generate the links of everything except the LINKTOEXCLUDE. Soz for caps
The_Butcher
did it work? did you find a solution?
The_Butcher