0

在我的 global.asax.cs 中的 Begin Reqeust 方法中,我想检查当前请求是否是 MVC 请求,而不是对 .css 文件或 .js 文件等的请求。

假设我有以下控制器:

/User/
/Product/
/Store/
/Checkout/

我想遍历控制器的名称,并验证当前请求 URL 是否用于上述控制器中的操作。

How can I look through the controller names?
4

2 回答 2

2

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.

于 2013-05-07T19:04:36.710 回答
1

您可以使用反射并从指定的命名空间获取所有控制器。

using System.Reflection;
private Type[] GetControllersInNamespace(Assembly assembly, string controllernamespace)
{
    return assembly.GetTypes().Where(types => string.Equals(types.Namespace, controllernamespace, StringComparison.Ordinal)).ToArray();
}
于 2013-05-07T18:32:49.663 回答