[英]How to overrite appsettings.json or appsettings.<envrionment>.json file(s) programtically using .net core web api with C#?
[英].NET Core Web API: Possible to assign controller route in appsettings.json config file?
我希望通过配置文件(appsettings.json)动态分配Web API控制器的路由。
我认为这可能是一个简单的解决方案。 但是,[毫无意外]尝试不起作用:
private static string route = Configuration.GetValue<string>("Path");
[Route(route)]
// action
结果导致An attribute argument must be a constant expression, typeof expression, or array creation expression of an attribute parameter type
我可以做我想做的吗? 如果是这样,那么最佳做法是什么? 我找不到很多文档,并且不希望自己滚动自己的HttpListener
来实现相对简单的目标。
原来我在想什么。
在Startup
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"});
});
}
易如反掌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.