Asp.net core 将Core 1.0配置为以net45为目标并运行IIS Express
如果您能使用最新的ASPNET Core 1.0工具帮助我正确配置MVC应用程序,使其面向.NET 4.5,我将不胜感激 从Microsoft.AspNet.Mvc 6 RC升级到Microsoft.AspNetCore.Mvc 1.0.0后,除了IISExpress的502.3响应之外,我无法获得任何其他响应 我猜我这里缺少一些基本配置,但是关于使用核心工具瞄准.NET4.5的文档很少 感谢您的帮助,提前感谢 这是我的project.json:Asp.net core 将Core 1.0配置为以net45为目标并运行IIS Express,asp.net-core,asp.net-core-mvc,asp.net-core-1.0,Asp.net Core,Asp.net Core Mvc,Asp.net Core 1.0,如果您能使用最新的ASPNET Core 1.0工具帮助我正确配置MVC应用程序,使其面向.NET 4.5,我将不胜感激 从Microsoft.AspNet.Mvc 6 RC升级到Microsoft.AspNetCore.Mvc 1.0.0后,除了IISExpress的502.3响应之外,我无法获得任何其他响应 我猜我这里缺少一些基本配置,但是关于使用核心工具瞄准.NET4.5的文档很少 感谢您的帮助,提前感谢 这是我的project.json: { "buildOptions": {
{
"buildOptions": {
"emitEntryPoint": true,
"compile": {
"exclude": [
"wwwroot",
"node_modules"
]
}
},
"configurations": {
"Development": {}
},
"dependencies": {
"Autofac": "4.0.0-rc1-177",
"Autofac.Extensions.DependencyInjection": "4.0.0-rc1-177",
"AutofacSerilogIntegration": "1.0.12",
"AutoMapper": "4.2.1",
"MediaTypeMap": "2.1.0",
"Microsoft.AspNetCore.Authentication.Cookies": "1.0.0",
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
"Microsoft.AspNetCore.Authentication.OpenIdConnect": "1.0.0",
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Http.Abstractions": "1.0.0",
"Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.FileProviderExtensions": "1.0.0-rc1-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Configuration.UserSecrets": "1.0.0-rc1-final",
"Microsoft.Extensions.DependencyInjection": "1.0.0",
"Microsoft.Extensions.Logging.Abstractions": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc2-final",
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final",
"Serilog.Extensions.Logging": "1.0.0",
"Serilog.Sinks.RollingFile": "2.1.0",
"Swashbuckle.SwaggerGen": "6.0.0-beta901",
"Swashbuckle.SwaggerUi": "6.0.0-beta901"
},
"frameworks": {
"net451": {}
},
"publishOptions": {
"exclude": [
"**.user",
"**.vspscc"
],
"include": [
"Areas",
"Views",
"wwwroot",
"appsettings.json",
"web.config"
]
},
"scripts": {
"prepublish": [ "npm install", "bower install", "gulp" ]
},
"tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-*",
"Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final"
},
"userSecretsId": "asdf",
"version": "1.0.0-*",
"webroot": "wwwroot"
}
以下是我的主要观点:
public static void Main(string[] args) {
var host = new WebHostBuilder()
.UseKestrel()
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
host.Run();
}
publicstaticvoidmain(字符串[]args){
var host=new WebHostBuilder()
.UseKestrel()
.Useii整合()
.UseStartup()
.Build();
host.Run();
}
Startup.cs:
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using AutofacSerilogIntegration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Converters;
using Serilog;
using Swashbuckle.Swagger.Model;
namespace Web
{
public class Startup
{
public Startup(IHostingEnvironment env)
{
var contentRootPath = env.ContentRootPath;
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.SetBasePath(contentRootPath)
.AddEnvironmentVariables()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true);
builder.AddEnvironmentVariables();
Configuration = builder.Build();
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
GlobalVariables.WebRootPath = env.WebRootPath;
var configSettings = new ConfigSettings(Configuration);
string logPath = configSettings.LoggingConfiguration.Path;
logPath = LoggingPathHelper.GetPath(logPath, env.WebRootPath);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.RollingFile(logPath)
.CreateLogger();
}
public IConfigurationRoot Configuration { get; set; }
// 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,
IApplicationLifetime appLifetime)
{
Serilog.SerilogLoggerFactoryExtensions.AddSerilog(loggerFactory);
appLifetime?.ApplicationStopped.Register(Log.CloseAndFlush);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
//CORE 1.0 upgrade: this doesn't appear to be used. Appears to be redundant to the work done in Identity project - asteffes
//try {
// using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
// .CreateScope()) {
// serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
// .Database.Migrate();
// }
//}
//catch {
//}
}
app.UseStaticFiles();
// Tell OWIN to use Cookies to save the Identity and other Claims information
var cookieAuthenticationOptions = new CookieAuthenticationOptions
{
AuthenticationScheme = "Cookies",
AutomaticAuthenticate = true
};
app.UseCookieAuthentication(cookieAuthenticationOptions);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap = new Dictionary<string, string>();
var options = new JwtBearerOptions
{
Authority = GlobalVariables.Settings.SecurityIdentityServerEndpointURI,
RequireHttpsMetadata = false,
Audience = GlobalVariables.Settings.SecurityIdentityServerEndpointURI + "resources",
AutomaticAuthenticate = true
};
app.UseMvc(routes =>
{
routes.MapRoute(
"areaRoute",
"{area:exists}/{controller}/{action}");
routes.MapRoute(
"default",
"{controller=App}/{action=Index}/{id?}");
});
// Add Swagger support
// https://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/
app.UseSwaggerUi();
}
// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
var exceptionNotifierFilter = new ExceptionNotifierFilter();
services.AddMvc(options => { options.Filters.Add(exceptionNotifierFilter); })
// Make all results be camel-case
.AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CustomCamelCasePropertyNamesContractResolver();
options.SerializerSettings.Converters.Add(
new StringEnumConverter {CamelCaseText = true});
});
// Add Swagger support
services.AddSwaggerGen();
// https://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/
var pathToDoc = Configuration["Swagger:Path"];
services.AddSwaggerGen();
services.ConfigureSwaggerGen(options =>
{
var info = new Info
{
Version = "v1",
Title = "API",
Description = "API",
};
options.SingleApiVersion(info);
options.IncludeXmlComments(pathToDoc);
var addFileUploadParams = new AddFileUploadParams();
options.OperationFilter<AddFileUploadParams>(addFileUploadParams);
options.DescribeAllEnumsAsStrings();
//options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(pathToDoc));
});
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
var autofacContainer = RegisterAutofac(services, Configuration);
var serviceProvider = autofacContainer.Resolve<IServiceProvider>();
// Save the ConfigSetttings instance in the Service Provider so it can be used elsewhere in the application.
// This is necessary because serviceProvider is no longer accessible from within the application.
GlobalVariables.Settings = serviceProvider.GetService<ConfigSettings>();
GlobalVariables.AutofacContainer = autofacContainer;
GlobalServiceVariables.AutofacContainer = autofacContainer;
InjectDependenciesInto(exceptionNotifierFilter, serviceProvider);
// Return the ServiceProvider created above based on AutofacModule
// NOTE: This changes the return value of Configuration to IServiceProvider
return serviceProvider;
}
private static void InjectDependenciesInto(ExceptionNotifierFilter exceptionNotifierFilter,
IServiceProvider serviceProvider)
{
// Have to use setter injection since the instance had to be created earlier
exceptionNotifierFilter.EmailService = serviceProvider.GetService<IEmailsService>();
exceptionNotifierFilter.ConfigSettings = serviceProvider.GetService<ConfigSettings>();
exceptionNotifierFilter.UserResolver = serviceProvider.GetService<IUserResolver>();
exceptionNotifierFilter.ExceptionLogEntryRepository =
serviceProvider.GetService<IEntityRepository<ExceptionLogEntry>>();
exceptionNotifierFilter.CryptographyService = serviceProvider.GetService<ICryptographyService>();
}
private static IContainer RegisterAutofac(IServiceCollection services, IConfigurationRoot configuration)
{
var builder = new ContainerBuilder();
// We need to pass in the web assembly and the IServiceFacade so that Autofac can register them;
// since web has a dependency on init, init cannot have a direct dependency back to web.
var maxAutofacModule = new MaxAutofacModule(configuration);
maxAutofacModule.IncludeServiceFacadeRegistrations(typeof(Startup).Assembly, typeof(IServiceFacade));
builder.RegisterModule(maxAutofacModule);
builder.Populate(services);
var container = builder.Build();
builder.RegisterLogger(Log.Logger);
return container;
}
}
使用系统;
使用System.Collections.Generic;
使用System.IdentityModel.Tokens.Jwt;
使用Autofac;
使用Autofac.Extensions.DependencyInjection;
使用自动传真集成;
使用Microsoft.AspNetCore.Builder;
使用Microsoft.AspNetCore.Hosting;
使用Microsoft.Extensions.Configuration;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.Extensions.Logging;
使用Newtonsoft.Json.Converters;
使用Serilog;
使用swashback.Swagger.Model;
命名空间Web
{
公营创业
{
公共启动(IHostingEnvironment环境)
{
var contentRootPath=env.contentRootPath;
//设置配置源。
var builder=new ConfigurationBuilder()
.SetBasePath(contentRootPath)
.AddenEnvironmentVariables()
.AddJsonFile(“appsettings.json”)
.AddJsonFile($“appsettings.{env.EnvironmentName}.json”,true);
builder.AddEnvironmentVariables();
Configuration=builder.Build();
if(env.IsDevelopment())
{
//有关使用用户机密存储的更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration=builder.Build();
GlobalVariables.WebRootPath=env.WebRootPath;
var configSettings=新的configSettings(配置);
字符串logPath=configSettings.LoggingConfiguration.Path;
logPath=LoggingPathHelper.GetPath(logPath,env.WebRootPath);
Logger.Logger=新的LoggerConfiguration()
.MinimumLevel.Information()
.WriteTo.RollingFile(日志路径)
.CreateLogger();
}
公共IConfigurationRoot配置{get;set;}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
public void Configure(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂、,
i应用程序生命周期(应用程序生命周期)
{
Serilog.seriloggerfactoryextensions.AddSerilog(loggerFactory);
appLifetime?.ApplicationStopped.Register(Log.CloseAndFlush);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
if(env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
//有关在部署期间创建数据库的更多详细信息,请参阅http://go.microsoft.com/fwlink/?LinkID=615859
//核心1.0升级:这似乎没有被使用。这似乎与Identity project-asteffes中完成的工作无关
//试一试{
//使用(var serviceScope=app.ApplicationServices.GetRequiredService()
//.CreateScope()){
//serviceScope.ServiceProvider.GetService()
//.Database.Migrate();
// }
//}
//抓住{
//}
}
app.UseStaticFiles();
//告诉OWIN使用Cookies保存身份和其他索赔信息
var cookieAuthenticationOptions=新的cookieAuthenticationOptions
{
AuthenticationScheme=“Cookies”,
自动验证=真
};
app.UseCookieAuthentication(cookieAuthenticationOptions);
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap=new Dictionary();
var选项=新的JWTBeareOptions
{
Authority=GlobalVariables.Settings.SecurityIdentityServerEndpointURI,
RequireHttpsMetadata=false,
访问群体=GlobalVariables.Settings.SecurityIdentityServerEndpointURI+“资源”,
自动验证=真
};
app.UseMvc(路由=>
{
routes.MapRoute(
“区域路线”,
“{area:exists}/{controller}/{action}”);
routes.MapRoute(
“默认”,
“{controller=App}/{action=Index}/{id?}”);
});
//添加昂首阔步的支持
// https://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/
app.UseSwaggerUi();
}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
公共IServiceProvider配置服务(IServiceCollection服务)
{
var exceptionNotifierFilter=新的exceptionNotifierFilter();
services.AddMvc(options=>{options.Filters.Add(exceptionNotifierFilter);})
//使所有结果都是骆驼式的
.AddJsonOptions(选项=>
{
options.SerializerSettings.ContractResolver=
新的CustomCamelCasePropertyNamesContractResolver();
options.SerializerSettings.Converters.Add(
新StringEnumConverter{CamelCaseText=true});
});
//添加昂首阔步的支持
services.addswagggen();
// https://damienbod.com/2015/12/13/asp-net-5-mvc