如何设置和使用ASP.net 5 RC1配置?

时间:2015-12-30 17:13:07

标签: configuration asp.net-core

我对ASP.net 5中新配置系统的工作方式感到很困惑。是的,我知道我创建了一个config.json,但是如何在我的代码中访问它的内容,特别是在Startup.cs中? / p>

许多教程使用的Configuration类似乎与Microsoft.Extensions.Configuration不存在。他们似乎使用Microsoft.Framework.ConfigurationModel。不是那个"不再生效"?我认为因为Intellisense没有给我一个1.0.0-rc1- *。

我找不到有关如何在ASP.net 5 RC1中使用配置的更新指南。有人请告诉我怎么做。

此外,我需要一些环境变量的帮助以及它们如何与新的配置框架交互,如果它们意味着以某种方式进行交互。我希望能够运行应用程序,并选择将其作为dev运行并将其部署为发行版。

1 个答案:

答案 0 :(得分:0)

在Startup.cs中你可以设置多个配置源,你可以使用像appsettings.json这样的json文件,你可以使用环境变量,你也可以使用一个名为usersecrets的新东西。添加它们的顺序很重要,因为每个配置源都可以复制相同的设置。 添加配置源后,它获得的优先级越高。从我自己的项目中考虑这个样本,并看到评论:

"Microsoft.Extensions.Configuration": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Abstractions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
"Microsoft.Extensions.OptionsModel": "1.0.0-rc1-final",
"Microsoft.Extensions.DependencyInjection.Abstractions": "1.0.0-rc1-final",

然后,就存储配置设置而言,您应该使用具有可在启动时从配置映射并在DI服务中注册的属性的类,以便可以将它们注入到您需要的任何位置。有关详细示例,请参阅this question

更新:以下软件包来自我的project.json,是必需/相关的软件包,智能感知是否有效:

//consider changing imageButtonToggle to a container view with an image in it
//in this example I'll use carrotImage as the image contained within imageButtonToggle
//formContainer contains all the views you're toggling from GONE to VISIBLE.
imageButtonToggle.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
                if (!isExpanded) {
                    ViewAnimationHelper
                            .expand(formContainer, 300, false);
                } else {
                    ViewAnimationHelper.collapse(formContainer, 300);
                }

                carrotImage.animate().rotation(ROTATION_COUNT);

                ROTATION_COUNT += 180f;
                //Silly check.. but better safe than sorry.
                if (ROTATION_COUNT >= Float.MAX_VALUE)
                    ROTATION_COUNT = 0f;

                isExpanded= !isExpanded;
            }
    });


public class ViewAnimationHelper {

/**
 * Easy way to expand a given view after measuring with
 * v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
 * 
 * @param v
 * @param duration
 */
public static void expand(final View v, int duration,
        boolean bStartFromZeroHeight) {
    v.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    if (bStartFromZeroHeight)
        v.getLayoutParams().height = 0;

    v.setVisibility(View.VISIBLE);
    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime,
                Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1 ? LayoutParams.WRAP_CONTENT
                    : (int) (targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration(duration);
    v.startAnimation(a);
}

/**
 * Easy way to just collapse any given view and any given speed
 * 
 * @param v
 * @param duration
 */
public static void collapse(final View v, int duration) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation() {
        @Override
        protected void applyTransformation(float interpolatedTime,
                Transformation t) {
            if (interpolatedTime == 1) {
                v.setVisibility(View.GONE);
            } else {
                v.getLayoutParams().height = initialHeight
                        - (int) (initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    a.setDuration(duration);
    v.startAnimation(a);
}
}