我希望通过配置文件 (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/