Reputation: 14919
In my Begin Reqeust method in my global.asax.cs, I want to check if the current request is a MVC request, and not a request for a .css file or .js file etc.
Say I have the following controllers:
/User/
/Product/
/Store/
/Checkout/
I want to loop through the names of the controllers, and verify the currently request URL is for an action in the above controllers.
How can I look through the controller names?
Upvotes: 0
Views: 371
Reputation: 13706
Based on what you want this for, your best solution will be routes.IgnoreRoute()
.
See here and here for various examples, but the basic premise is that you want the MVC engine to accept the route, but then it says "I'm not supposed to do anything with this" and lets it go back to IIS to find the actual file.
This means you don't need to try and determine on the fly what controllers you have, which is much easier on your server.
Upvotes: 2
Reputation: 50503
You could use reflection and get all the controllers from a specified namespace.
using System.Reflection;
private Type[] GetControllersInNamespace(Assembly assembly, string controllernamespace)
{
return assembly.GetTypes().Where(types => string.Equals(types.Namespace, controllernamespace, StringComparison.Ordinal)).ToArray();
}
Upvotes: 1