- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在单独的类库中开发 MVC API。 API 方法使用属性路由。该 API 将被其他 MVC 应用程序使用(不是我构建的)。
主 MVC 应用程序将引用我的库程序集并在它自己的启动类中调用 AddMvc()
/UseMvc()
。它将能够为我的 API 库动态设置根 API url(从配置或选项设置委托(delegate)),这样它就可以确保与它自己的路由没有冲突,它可以使用属性路由或集中式路由。
假设我的 API 库有一个 product/{id}
路由。主应用程序应该能够选择任何路由前缀,例如 api/product/{id}
或 some/other/prefix/product/{id}
。
在启动时,MVC 会发现所有引用程序集中的所有 Controller /路由,它还会发现并注册我的 API 库路由,但仅限于硬编码的 product/{id}
路由,没有任何前缀。
我一直在尝试让 MVC 使用前缀注册路由,但到目前为止没有成功。主应用程序将调用自定义 AddMyApi()
/UseMyApi()
配置方法,因此我可以为我的库进行配置/设置。我尝试过的一些事情:
映射
app.Map("/custom-prefix", api =>
{
api.UseMvc();
});
这将导致 custom-prefix/product/{id}
和 product/{id}
的路由重复。
路由约定
基于 http://www.strathweb.com/2016/06/global-route-prefix-with-asp-net-core-mvc-revisited/
services.AddMvc(options =>
{
options.Conventions.Insert(0, new RouteConvention(new RouteAttribute("custom-prefix")));
});
看起来这行不通,因为选项将被主应用程序对 AddMvc()
的调用覆盖,反之亦然,具体取决于哪个先被调用。
自定义路由属性
基于 Controller 类上的 IRouteTemplateProvider
的自定义路由属性将不起作用,因为我需要从选项类注入(inject)的前缀,并且属性不支持构造函数注入(inject)。
延迟路由发现
基于 http://www.strathweb.com/2015/04/asp-net-mvc-6-discovers-controllers/
我已将 [NonController]
添加到库 Controller ,以防止它们在主应用程序启动时被发现。但是我后来无法添加它们,而且我想我会遇到主应用程序再次覆盖 MVC 选项的相同问题。
区域
我不能使用区域,因为主应用程序可能决定从根(没有前缀)运行 API。
所以我对如何解决这个问题感到困惑。感谢您的帮助。
最佳答案
我认为这里的约定是正确的方法,而您缺少的一点只是为要在 MVC 中注册的库提供适当的扩展方法。
首先创建一个约定,为所有通过特定选择器的 Controller 添加一个前缀。
AttributeRouteModel
或添加一个新的(如果未找到)。这将是此类约定的示例:
public class ApiPrefixConvention: IApplicationModelConvention
{
private readonly string prefix;
private readonly Func<ControllerModel, bool> controllerSelector;
private readonly AttributeRouteModel onlyPrefixRoute;
private readonly AttributeRouteModel fullRoute;
public ApiPrefixConvention(string prefix, Func<ControllerModel, bool> controllerSelector)
{
this.prefix = prefix;
this.controllerSelector = controllerSelector;
// Prepare AttributeRouteModel local instances, ready to be added to the controllers
// This one is meant to be combined with existing route attributes
onlyPrefixRoute = new AttributeRouteModel(new RouteAttribute(prefix));
// This one is meant to be added as the route for api controllers that do not specify any route attribute
fullRoute = new AttributeRouteModel(
new RouteAttribute("api/[controller]"));
}
public void Apply(ApplicationModel application)
{
// Loop through any controller matching our selector
foreach (var controller in application.Controllers.Where(controllerSelector))
{
// Either update existing route attributes or add a new one
if (controller.Selectors.Any(x => x.AttributeRouteModel != null))
{
AddPrefixesToExistingRoutes(controller);
}
else
{
AddNewRoute(controller);
}
}
}
private void AddPrefixesToExistingRoutes(ControllerModel controller)
{
foreach (var selectorModel in controller.Selectors.Where(x => x.AttributeRouteModel != null).ToList())
{
// Merge existing route models with the api prefix
var originalAttributeRoute = selectorModel.AttributeRouteModel;
selectorModel.AttributeRouteModel =
AttributeRouteModel.CombineAttributeRouteModel(onlyPrefixRoute, originalAttributeRoute);
}
}
private void AddNewRoute(ControllerModel controller)
{
// The controller has no route attributes, lets add a default api convention
var defaultSelector = controller.Selectors.First(s => s.AttributeRouteModel == null);
defaultSelector.AttributeRouteModel = fullRoute;
}
}
现在,如果这是您正在编写的应用程序的全部部分而不是库,您只需将其注册为:
services.AddMvc(opts =>
{
var prefixConvention = new ApiPrefixConvention("api/", (c) => c.ControllerType.Namespace == "WebApplication2.Controllers.Api");
opts.Conventions.Insert(0, prefixConvention);
});
然而,由于您提供的是一个库,您想要的是提供一个扩展方法,如 AddMyLibrary("some/prefix")
,它将负责添加此约定和任何其他设置,如注册所需的服务。
因此您可以为IMvcBuilder
编写扩展方法并更新该方法中的MvcOptions
。好处是因为它是 IMvcBuilder
的扩展,它总是在默认的 AddMvc()
之后被调用:
public static IMvcBuilder AddMyLibrary(this IMvcBuilder builder, string prefix = "api/")
{
// instantiate the convention with the right selector for your library.
// Check for namespace, marker attribute, name pattern, whatever your prefer
var prefixConvention = new ApiPrefixConvention(prefix, (c) => c.ControllerType.Namespace == "WebApplication2.Controllers.Api");
// Insert the convention within the MVC options
builder.Services.Configure<MvcOptions>(opts => opts.Conventions.Insert(0, prefixConvention));
// perform any extra setup required by your library, like registering services
// return builder so it can be chained
return builder;
}
然后您会要求您图书馆的用户将其包含在他们的应用程序中,如下所示:
services.AddMvc().AddMyLibrary("my/api/prefix/");
关于c# - 单独库中 Controller 的动态路由前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42364745/
Java 专家需要您的帮助。 今天我在一次采访中被问到这个问题,但我无法解决。所以我需要一个解决方案来解决这个问题; 反转字符串 Input : Hello, World! Output : oll
目标:单击按钮并将成分作为单独的项目添加到数组中。 当前设置: 这给出:蓝莓芒果柠檬汁 然后我希望能够通过单击按钮将成分作为单独的项目添加到数组中: var allI
如何编写正则表达式来匹配它(参见箭头): "this is a ->'' this is a "test' there is another "test' 第二种情况 /\b'/ Regex Demo
我有一个数组,其中包含有限数量的项目。我想随机删除项目,直到所有项目都被使用过一次。 示例 [1,2,3,4,5] 使用了随机数 5,所以我不想再这样了。使用了随机数 2,所以我不想再这样了。等等..
首先,抱歉,如果这太主观了,我只是不知道还能怎么问/去哪里问。 无论如何,鉴于我最近的所有问题,我准备很快发布一个 Android 应用程序,并且大部分测试都是在我的手机 Droid 上完成的。我真的
这可能不是这个问题的正确位置,如果不合适请随意移动它。我标记为 Delphi/Pascal 因为这是我在 atm 中工作的内容,但这可能适用于我猜的所有编程。 无论如何,我正在做一些代码清理,并考虑将
我像这样分隔了其余 api 的路由。有没有更好的方法来组织路由器?还是我现在的做法没问题? app.js app.use('/api/auth',auth); 应用程序/ Controller /au
我在 2 个单独的工作表中包含以下数据: 表1: A B C D a ff dd ff ee b 12 10 10 12 表2: A B C
我正在使用 jQuery,并在位于单独 HTML 文件中的表中获取了几行。单击时,每一行都会成功重定向到本地 HTML 文件。 (使用window.location) 我想要实现的目标 我想要完成的是
我有重叠背景图像的问题,当它们重叠时会导致阴影比不重叠时更暗,从而产生不均匀的阴影。 我有一个高度灵活的盒子,带有一些透明的背景图像和阴影以创建漂亮的边框。盒子本质上是 3 个元素。 您可以在此处找到
按照正常的微服务框架,我们希望将每个微服务放入其自己的 git 存储库中,然后为 Service Fabric 项目创建一个存储库。当我们更新其中一个微服务时,Service Fabric 项目将仅重
我想将多个片段嵌入到一个指令中。这是我的设置方式。 Everyone Development (3)
我希望在保留原件的同时将多个文件 gzip 到一个目录中(到多个 .gz 文件中)。 我可以使用这些命令来处理单个文件: find . -type f -name "*cache.html" -exe
有没有办法分别知道每个 Eclipse 插件消耗了多少内存? 最佳答案 进行堆转储并使用例如分析它Eclipse Memory Analyser . 如需更多信息,请参阅 Analyzing Equi
我们使用cusrom插件并以这种方式定义脚本(这是一个近似的伪代码): //It is common part for every script (1) environments { "env1"
我在控制台应用程序中托管了一个集线器,并有一个 WPF 应用程序连接到它。它工作得很好。然后我将集线器移到一个单独的项目中,并将主机的引用添加到新项目中。现在我收到 500 错误,没有其他详细信息。
是否可以在单独的 JAR 文件中为 JavaBean 构建类?具体来说,JavaBean 在一个 JAR 文件中具有 Bean 和 BeanInfo 类,而自定义属性编辑器类位于另一个 JAR 文件中
好的,所以我有一个 MAF 应用程序,它在单独的应用程序域中加载每个插件。这非常适合我的需要,因为它允许我在运行时动态卸载和重新加载我的插件。 问题是,我需要能够在子应用域中处理未处理的异常,捕获它,
在参加在线数据库类(class)(针对初学者)时,我注意到一个问题,我必须查找涉及...至少两个不同值的查询...例如, ELMASRI 书中的 COMPANY 数据库指出:查找至少从事两个不同项目的
(首先:我已经尝试了涉及边距、边框等的所有选项。) Link to problematic page. Link to similarly constructed, non-problematic p
我是一名优秀的程序员,十分优秀!