Removing Specific Routes from ASP.NET 4 RoutesTable - asp.net

my site has many many routes. since the users can add or remove pages at will, i need a way to reregister the routes from time to time
at reregister i do not want to clear the whole route cache ("RouteTable.Routes.Clear"), but i would rather go thru the table route by route, and using a certain regex on its name, decide if to remove it or not.
after that i will reregister the specific pages that i need to
heres my code meanwhile
For Each r In RouteTable.Routes
If CType(r, Route).DataTokens("ConfigID") = ConfigID Then RouteTable.Routes.Remove(r)
Next
after the first remove it throws an error "Collection was modified; enumeration operation may not execute."
thank you very much for your help!!

It's not possible to get the route name of the route because the name is not a property of the Route. When adding routes to the RouteTable, the name is used as an internal index for the route and it's never exposed.
There's one way to do this.
When you register a route, set a DataToken on the route with the route name and use that to filter routes.
The easiest way to do #1 is to probably write your own extension methods for mapping routes.

Related

Why do we need custom route if we can define globally?

If I can add routes in global.ascx or similar file then why do we need custom router i.e. the routes over individual WebApi methods. When it's needed? What is the benefit?
[System.Web.Http.HttpPost]
[Route("api/AndroidOperations/AddManualAppointment")]
public object AddManualAppointment(AddManualBookingViewModel AddManualBookingVM)
{
BookingsRepository BookingRep= new BookingsRepository();
int ReturnRowsCount = BookingRep.InsertManualCustomerAppointments(AddManualBookingVM, out ReturnStatus, out ReturnMessage, out ReturnBookingID);
return new {ReturnMessage= ReturnMessage, ReturnStatus= ReturnStatus};
}
Attribute Routing is a more flexible solution than Convention Routing, if only because it allows you quite a bit more flexibility and places the routes next to the actions that will actually use them.
Consider choosing traditional routes when:
You want centralized configuration of all your routes.
You use custom constraint objects.
You have an existing working application you don’t want to change.
Consider choosing attribute routes when:
You want to keep your routes together with your action’s code.
You are creating a new application or making signifi cant changes to an existing one.
Attribute routing nicely keeps everything about your controllers together, including both the URLs they use and the actions that run. However there are certainly benefits to using both in tandem, particularly in situations when you know how some routes will look but aren't sure about others.
Note: Attribute Routing is more specific and overrides Convention Routing. Be sure attribute routing should be defined first to convention based routing.
To find out more see docs and Attribute Routing vs Convention Routing

Symfony2 dynamically select controller based on query result

I'm trying to create an extendible cms. I have URLs in my database like "/menu-item/content" for each of these urls there is an action specified in format "AcmeDemoBundle:Default:index".
I would like to resolve this action and call the appropriate controller based on the matched URL.
My main goal is to call the controller stored in the db directly. Before i used a method when i put a route in the end of the routings which matched all urls associate a controller with it that found out the target controller from the db, than used Controller::forward() to get the expected results. The main problem with this approach is that i need to create an unnecessary subrequest for each page load. The other problem is passing post variables to this new subrequest.
So far i've tried to listen on kernel.request event and modify $request->attributes to contain the matching _controller value, but it seems like it has no effect.
Any advice in the subject is appreciated.
I think you mixed up the forward and redirect calls. Redirect performs 301 or 302 redirects (permanent and temporary respectfully), which creates the subrequest, as you call it. Forward performs internal call (same request) to the new action. As for your second problem, as forward is an internal call within the same request, everything global stays the same (including POST variables). In my opinion, using forward might be a good solution for you (if you don't have any other low-level requirements or if you want to be even more flexible than the usual MVC operation allows).
Hope this helps.

Is it possible to have catchall followed by other parameters in a ASP.NET MVC Route?

I would like to create a route that looks something like this:
routes.Add(new Route("{*url}/{action}.do/{id}", new MvcRouteHandler())
Is this possible? It seems like the catchall has to be last?
The catch-all has to be the last parameter in the route, as it says "match the entirety of what remains of the URL."
You could fake it, though, by having just the catchall and using a custom MyRouteHandler instead of the MvcRouteHandler. Your custom route handler would just manipulate the RouteContext to split the action and id back out of the URL before passing it to the MvcRouteHandler for processing.
From ScottGu:
The MVC framework chooses the
Controller to use by evaluating the
RouteTable rules in the order that
they have been registered
I think you can register after the catch-all, but it won't ever be hit because the catch-all will be hit first.

ASP.NET MVC View information stored in a data-store

I'm looking for some advice on storing views in a data-store (database, file, other) and display them based on routing data, all using ASP.NET MVC 2 and ASP.NET Routing.
For example, I'd like to be able to display different views based on the following route data:
/{country}/
/{country}/{area}
But in the same vein I'd like to display:
/{planet}/
/{planet}/{satellite}
All are based on strings, and the data isn't fixed. So based on the number of segments maybe, use that as the selection criteria into the data-store...additionally, I may not know the segments up front, so they'd all be dynamic.
I'm was hoping we could get a few different methods together here, as kind of a reference for all - I'm sure some methods won't suite everyone...
So, how would you do it?
Branislav Abadjimarinov suggested a Controller Factory which could be used to do the look-up and display the page dynamically. I like this idea, what do you think?
There is no way for MVC to understand from this url's which route to choose. You have to make the routes more specific. For example:
/planet/{planet}/{satelite}
/country/{country}/{area}
You also have the option to define your own controller factory. The controller factory decides which controller to instantiate based on the route. So you can put some custom logic in it like - check if the {planet} parameter exist and if yes instantiate Planet controller else instantiate Countries controller.
This Post could be really helpful for you.
Remember you always can add a new routing rule : )
Just like this

Custom catch-all parameter in routing

I recently want to have a special routing rule : {*whatever}/details/{Id}/{itemName}
I know an exception will be thrown once I run the application. In my application, for example my url pattern is www.domain.com/root/parent/child/.../child/details/30/itemname
but the current routing doesnot support this. How can custom the routing handler to make it work?
A class has been written that supports this
I've written such a class that can handle catch-all segment anywhere in the URL. There's quite some code to it, but it works as expected and I've used it on a real life project.
Check it out yourself and see if it fulfils your needs.
The problem is... how will it know when to stop?
the {*whatever} segment will match:
/foo/
/foo/bar
/foo/bar/details/4/moreFoo
/foo/bar/andmore/details/4/moreFoo
Because the catch-all parameter includes anything, it will never stop.
The only way to implement this would be to create a different route for each place you use details...
eg:
games/details/{id}/{itemName}
widgets/details/{id}/{itemName}
books/details/{id}/{itemName}
Of course, that is already provided in the default {controller}/{action}/{id} route
I think you may want to look at extending the System.Web.Routing.RouteBase class and override the GetRouteData() method. With it you can look at the requested url and decide if matches your pattern and if so construct and return a new instance of RouteData that points to the controller and action that you want to handle the request. Otherwise if you don't match the requested url you return null.
See the following for examples:
Pro ASP.NET MVC Framework
By Steve Sanderson
Custom RouteBase

Resources