这篇文章主要为大家详细介绍了asp.net core项目配置教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
在这一章,我们将讨论 asp.net core项目的相关的配置。在解决方案资源管理器中,您将看到 startup.cs 文件。如果你有以前版本的 asp.net的工作经验,你可能希望看到一个 global.asax 文件,您可以在其中编写代码,它是一个编写程序启动时立即执行的代码的文件。
你可能也希望看到一个 web.config 文件,该文件包含您的应用程序执行所需的所有配置参数。
在 asp.net core中,那些文件都没了,取而代之的是 startup.cs文件.
startup.cs里面是一个启动类文件,并在该类中您可以配置您的应用程序甚至配置您的配置资源。
这里是 startup.cs 文件中的默认实现代码:
using system;
using system.collections.generic;
using system.linq;
using system.threading.tasks;
using microsoft.aspnetcore.builder;
using microsoft.aspnetcore.hosting;
using microsoft.aspnetcore.http;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.logging;
namespace firstappdemo {
public class startup {
// this method gets called by the runtime.
// use this method to add services to the container.
// for more information on how to configure your application,
// visit http://go.microsoft.com/fwlink/?linkid=398940
public void configureservices(iservicecollection services) {
}
// this method gets called by the runtime. use this method to configure
// the http request pipeline.
public void configure(iapplicationbuilder app, ihostingenvironment env,
iloggerfactory loggerfactory) {
loggerfactory.addconsole();
if (env.isdevelopment()) {
app.usedeveloperexceptionpage();
}
app.run(async (context) => {
await context.response.writeasync("hello world!");
});
}
}
}
在启动类中,我们的大部分工作将设计有两种方法。configure 方法是构建http处理管道的地方。
这定义了应用程序如何响应请求。目前该应用程序只能说“hello world!”如果我们希望该应用程序具有不同的行为,我们需要通过添加额外的代码到这个configure方法中来改变周围的管道。
例如,如果我们想要提供一个 index.html 文件的静态文件,我们将需要在configure方法中添加一些代码。
你也可以有一个错误页面或asp.net controller的异常请求的路由;这两个场景还需要在这个配置方法中做一些工作。
在启动类中,您还将看到 configureservices() 方法。这可帮助您配置您的应用程序的组件。
现在,我们有一个硬编码的字符串“hello world !”来响应每个请求。我们不希望每个请求都是硬编码的字符串,我们想从一些组件加载响应字符串。
其他组件可能会从数据库加载文本,或从一个web服务或一个json文件,我们不管这它是从什么地方加载。
我们会设置一个场景,这样我们就没有这个硬编码字符串了。
在解决方案资源管理器中,右键单击您的项目节点并选择add→new item。
在左侧窗格中,选择installed → code,然后在中间窗格中,选择json文件。给这个文件取名为appsetting.json,并单击add按钮如上面的截图。
让我们在appsettings中添加以下代码。
{
"message": "hello, world! this message is from configuration file..."
}
现在我们需要从 startup.cs 文件访问此消息。这里是 startup.cs 文件从 json 文件阅读上面的消息的实现代码。
using microsoft.aspnet.builder;
using microsoft.aspnet.hosting;
using microsoft.aspnet.http;
using microsoft.extensions.dependencyinjection;
using microsoft.extensions.configuration;
namespace firstappdemo {
public class startup {
public startup() {
var builder = new configurationbuilder()
.addjsonfile("appsettings.json");
configuration = builder.build();
}
public iconfiguration configuration { get; set; }
// this method gets called by the runtime.
// use this method to add services to the container.
// for more information on how to configure your application,
// visit http://go.microsoft.com/fwlink/?linkid=398940
public void configureservices(iservicecollection services) {
}
// this method gets called by the runtime.
// use this method to configure the http request pipeline.
public void configure(iapplicationbuilder app) {
app.useiisplatformhandler();
app.run(async (context) => {
var msg = configuration["message"];
await context.response.writeasync(msg);
});
}
// entry point for the application.
public static void main(string[] args) =7gt; webapplication.run<startup>(args);
}
}
让我们现在运行应用程序。一旦您运行该应用程序,它会产生下面的输出。
以上就是asp.net core项目配置教程(6)_实用技巧的详细内容。