c# - .NET Core Web API : Possible to assign controller route in appsettings. json 配置文件?

标签 c# asp.net-core asp.net-core-mvc asp.net-core-routing

我希望通过配置文件 (appsettings.json) 动态分配我的 Web API Controller 的路由。

我认为这可能是一个简单的解决方案。然而,尝试这个 [毫不奇怪] 不起作用:

private static string route = Configuration.GetValue<string>("Path");

[Route(route)]
// action

这导致 属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式

有没有可能做我想做的事?如果是这样,这样做的最佳做法是什么?我找不到太多关于它的文档,并且不想使用我自己的 HttpListener 来完成一个相对简单的目标。

最佳答案

原来是我想多了。

启动

public void Configure(IApplicationBuilder app)
{
    // basic stuff
    var config = services.GetService<IConfiguration>();
    string route = config.GetValue<string>("Path");

    app.UseMvc(routes => {
        routes.MapRoute("someName", route, new { controller = "myController", action = "myAction"});
    });
}

简单易行。

关于c# - .NET Core Web API : Possible to assign controller route in appsettings. json 配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49010754/

相关文章:

c# - 如何使用反射创建高性能 .NET 类?

c# - 为什么 Swashbuckle.aspnet.core.swagger 不被识别

c# - 模型绑定(bind)忽略具有 JsonIgnore 属性的属性

visual-studio-2017 - 尝试创建新的空 Controller 时出错 : 'Sequence contains no matching element'

c# - 在两个单独的空隙之间传达错误 (C#)

c# - PureAttribute 对参数的用途

c# - 装箱/拆箱 - 只有值类型? Ref.types - 类型转换?

c# - 您可以将 Microsoft.AspNet.SignalR.Client 与 .NET Core (dnxcore) 一起使用吗

c# - 如何从 SignalR 获取连接客户端的域

c# - 将现有程序集添加到 mvc 6 项目