tags:

views:

61

answers:

1

I'm using ASP.NET MVC2 RC and I have built security on top of the Areas/Controller/Action specification, using basically a table that tells the infrastructure which role has permission to execute which controller action.

The code I used to get the "area" was this

RouteData.Values["area"]

And then I checked that in the Database. My problem is that when I migrated from MVC 1 RTM to MVC2 RC, the area goes in the DataTokens collection, and if the controller that is being called is in the root area, the following code returns null

RouteData.DataTokens["area"]

Do you know if there's any way to tell MVC that if "area" is not in the DataTokens collection, it should have string.Empty?

I'm trying to avoid modifying my code to check that for null.

Thanks!

A: 

Why not simply check if RouteData.DataTokens["area"] is null (or empty) and assume the default area when it is?

Edit

Apologies, I didn't read the the last line of your question before answering. What's the issue with modifying the code?

Nathan Taylor
Because I'm using it in a lot of places. But that's what I'll do, I have to change RouteData.Values for RouteData.DataTokens anyway
sabanito
If you have to change it in a lot of places you may want to take another look at how and when you're performing the evaluation. A simple ActionFilter on your [base] controller would easily do the job for all of the affected actions if implemented correctly. Good luck!
Nathan Taylor
Thx Nathan for the comments, finally I created an extension method on ControllerContext that gives me Area and changed every piece of code I had with this (not much code because it's the security infrastructure). This way if Area changes again, I have a single place to change it!
sabanito
Glad to assist.
Nathan Taylor