在本文中,我们将带你了解asp.net-core–如何在我的ASP.NET核心WebApi项目中全局启用CORS在这篇文章中,我们将为您详细介绍asp.net-core–如何在我的ASP.NET核心W
在本文中,我们将带你了解asp.net-core – 如何在我的ASP.NET核心WebApi项目中全局启用CORS在这篇文章中,我们将为您详细介绍asp.net-core – 如何在我的ASP.NET核心WebApi项目中全局启用CORS的方方面面,并解答asp.net core 3全栈web开发常见的疑惑,同时我们还将给您一些技巧,以帮助您实现更有效的.net-core – 如何创建我可以从.NET核心应用程序(Web API)引用的.NET核心库、.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置、.NET6-Asp.Net Core webapi -从零开始的webapi项目、ASP.NET Core 2.2 WebApi 系列【一】搭建ASP.NET Core WebApi项目。
本文目录一览:- asp.net-core – 如何在我的ASP.NET核心WebApi项目中全局启用CORS(asp.net core 3全栈web开发)
- .net-core – 如何创建我可以从.NET核心应用程序(Web API)引用的.NET核心库
- .net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置
- .NET6-Asp.Net Core webapi -从零开始的webapi项目
- ASP.NET Core 2.2 WebApi 系列【一】搭建ASP.NET Core WebApi项目
asp.net-core – 如何在我的ASP.NET核心WebApi项目中全局启用CORS(asp.net core 3全栈web开发)
我在Microsoft网站上找到了这个文档,我将其用作参考 – https://docs.asp.net/en/latest/security/cors.html
正如“在MVC中启用CORS”一节中所述,我试图在这样的ConfigureServices menthod中全局启用cors –
services.Configure<Mvcoptions>(options => { options.Filters.Add(new CorsAuthorizationFilterFactory("AllowSpecificOrigin")); });
但是,我收到了这个我无法理解的错误 –
The call is ambiguous between the following methods or properties:
‘Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection,
System.Action)’ and
‘Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection,
System.Action)’
请参阅错误截图 – ERROR scrrenshot
有谁能告诉我如何在我的ASP.NET核心WebApi项目中全局启用CORS?
Project.json:
{ "userSecretsId": "aspnet5-MVC6","version": "1.4.0-*","buildOptions": { "emitEntryPoint": true,"preserveCompilationContext": true },"dependencies": { "Microsoft.ApplicationInsights.AspNetCore": "1.0.0","Microsoft.AspNetCore.Authentication.Cookies": "1.0.0","Microsoft.AspNetCore.Diagnostics": "1.0.0","Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore": "1.0.0","Microsoft.AspNetCore.Identity": "1.0.0","Microsoft.AspNetCore.Identity.EntityFrameworkCore": "1.0.0","Microsoft.AspNetCore.Mvc": "1.0.0","Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0","Microsoft.AspNet.Cors": "6.0.0-rc1-final","Microsoft.AspNetCore.Server.IISIntegration": "1.0.0","Microsoft.AspNetCore.Server.Kestrel": "1.0.0","Microsoft.AspNetCore.StaticFiles": "1.0.0","Microsoft.EntityFrameworkCore": "1.0.0","Microsoft.EntityFrameworkCore.sqlServer": "1.0.0","Microsoft.Extensions.Configuration.CommandLine": "1.0.0","Microsoft.Extensions.Configuration.FileExtensions": "1.0.0","Microsoft.Extensions.Configuration.Json": "1.0.0","Microsoft.Extensions.Configuration.UserSecrets": "1.0.0","Microsoft.Extensions.Logging": "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" },"tools": { "BundlerMinifier.Core": "2.0.238","Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final","Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final","Microsoft.Extensions.SecretManager.Tools": "1.0.0-preview2-final" },"frameworks": { "netcoreapp1.0": { "imports": [ "dnxcore50" ],"dependencies": { "Microsoft.NETCore.App": { "type": "platform","version": "1.0.0" } } },"net461": { "dependencies": { "Microsoft.VisualStudio.Web.CodeGenerators.Mvc": "1.0.0-preview2-final" } } },"publishOptions": { "exclude": [ "**.user","**.vspscc","wwwroot","node_modules" ] },"scripts": { "prepublish": [ "npm install","bower install","gulp clean","gulp min" ] } }
解决方法
“Microsoft.AspNet.Cors”: “6.0.0-rc1-final”
是一个非常旧的版本,导致您的解决方案加载了两个具有相同名称空间和类型的不同程序集,编译器不知道使用哪个程序集.
将其更改为
“Microsoft.AspNetCore.Cors”: “1.0.0”
所有Microsoft.AspNet.*包都非常陈旧,不应使用.他们都用RC2重命名为Microsoft.AspNetCore.*
.net-core – 如何创建我可以从.NET核心应用程序(Web API)引用的.NET核心库
如果我创建另一个.NETCoreApp类型的项目,我可以引用它并使用这些类没有问题.
如何从.NET Core App中使用.NET Core类库?
编辑/更新:
>这似乎只是一个编辑器/智能感知问题,因为尽管编辑器警告,.NETCoreApp确实构建并运行,调用类库.
>我正在运行Resharper,我认为这与其他类型的项目存在类似的问题:我已经检查过我有最新版本并清除了Resharper缓存并重新启动了VS2105.
解决方法
有两种可能的解决方案:
>卸载Resharper,Visual Studio本机intellisense工作.
>安装Resharper 2016.2 EAP(早期访问程序)版本.我做到了这一点,它正在发挥作用.显然,它附带任何EAP / beta产品的警告.
这是Jetbrains forum post的链接,我被告知.NET Core 1尚未支持并指向EAP版本.
.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置
我想出了这个:
var configString = @"akka { log-config-on-start = on stdout-loglevel = INFO loglevel = DEBUG loggers= ""[Akka.Logger.Serilog.SerilogLogger,Akka.Logger.Serilog]"" actor { debug { receive = on autoreceive = on lifecycle = on event-stream = on unhandled = on } } akka.persistence { journal { plugin = ""akka.persistence.journal.sqlite"" sqlite {Akka.Persistence.sqlite.Journal.sqliteJournal,Akka.Persistence.sqlite"" plugin-dispatcher = ""akka.actor.default-dispatcher"" connection-string = ""Data Source = F:\\sqliteDb\\Sample.db3"" table-name = event_journal Metadata-table-name = journal_Metadata auto-initialize = on } } snapshot-store { plugin = ""akka.persistence.snapshot-store.sqlite"" sqlite {[Akka.Persistence.sqlite.Snapshot.sqliteSnapshotStore,Akka.Persistence.sqlite]"" connection-string = ""Data Source = F:\\sqliteDb\\Sample.db3"" table-name = snapshot_store auto-initialize = on } } } "; var config = ConfigurationFactory.ParseString(configString); ActorSystem.Create("AkkaSystem",config);
哪个没有按预期工作.
我们如何使用appsetting.json在Asp.Net核心中配置akka.net?或者有更好的方法吗?
解决方法
var config = ConfigurationFactory.FromObject(new { akka = configuration.GetSection("Akka").Get<AkkaConfig>() }); actorSystem = ActorSystem.Create("Stimpy",config);
请注意,我没有费心去弄清楚如何从appsettings解析kebab-case属性.所以我刚刚重命名了连字符以外的属性.然后将JsonProperty属性设置为正确的名称,以便FromObject可以正确地反序列化它.
public class AkkaConfig { [JsonProperty(PropertyName = "log-config-on-start")] public string logconfigonstart { get; set; } [JsonProperty(PropertyName = "stdout-loglevel")] public string stdoutloglevel { get; set; } public string loglevel { get; set; } public string[] loggers { get; set; } public ActorConfig actor { get; set; } public class ActorConfig { public DebugConfig debug { get; set; } public Dictionary<string,string> serializers { get; set; } [JsonProperty(PropertyName = "serialization-bindings")] public Dictionary<string,string> serializationbindings { get; set; } public class DebugConfig { public string receive { get; set; } public string autoreceive { get; set; } public string lifecycle { get; set; } [JsonProperty(PropertyName = "event-stream")] public string eventstream { get; set; } public string unhandled { get; set; } } } }
appsettings.json:
{ "Logging": { "IncludeScopes": false,"LogLevel": { "Default": "Trace" } },"Akka": { "logconfigonstart":"on","stdoutloglevel":"INFO","loglevel": "DEBUG","loggers": [ "Akka.Logger.NLog.NLogLogger,Akka.Logger.NLog" ],"actor": { "debug": { "receive": "on","autoreceive": "on","lifecycle": "on","eventstream": "on","unhandled": "on" },"serializers": { "hyperion": "Akka.Serialization.HyperionSerializer,Akka.Serialization.Hyperion" },"serializationbindings": { "System.Object": "hyperion" } } } }
.NET6-Asp.Net Core webapi -从零开始的webapi项目
本项目为本人22年毕设项目,后续会不断更新本篇文章,全部内容都会写在这一篇文章里,喜欢的请持续关注。
一、如何创建 Asp.Net Core webapi 项目
二、如何使用 EntityFrameWorkCore DbFirst:
需要用到的程序包:
1.数据库创建好表
2.程序包管理器控制台-执行命令
Scaffold-DbContext "server=localhost;port=3306;user=root;password=a.1234;database=lrms;Persist Security Info=True;" Pomelo.EntityFrameworkCore.MysqL -o Models -f
3.执行成功后会在项目下生成你指定的的文件夹(这里是命令中的Models),里面会生成对应的表类和一个连接数据库的上下文类(这里是 “数据库名+Context” )。
ASP.NET Core 2.2 WebApi 系列【一】搭建ASP.NET Core WebApi项目
一、步骤
从“文件”菜单中选择“新建”>“项目” 。
选择“ASP.NET Core Web 应用程序”模板,再单击“下一步” 。
将项目命名为 NetCoreWebApi,然后单击“创建” 。
选择“.NET Core”和“ASP.NET Core 2.2” 。 选择“API”模板,然后单击“创建” 。
创建完成后,项目结构如下:
二、项目解读
Properties——launchSettings.json
启动配置文件,一个ASP.NET Core应用保存特有的配置标准,用于应用的启动准备工作,包括环境变量,开发端口等。
1 {
2 "$schema": "http://json.schemastore.org/launchsettings.json",
3 "iisSettings": { //选择以IIS Express启动
4 "windowsAuthentication": false, //是否启用windows身份验证
5 "anonymousAuthentication": true, //是否启用匿名身份验证
6 "iisExpress": {
7 "applicationUrl": "http://localhost:60953", //IIS Express随机端口
8 "sslPort": 0
9 }
10 },
11 "profiles": {
12 "IIS Express": {
13 "commandName": "IISExpress",
14 "launchBrowser": true, //是否在浏览器中启动
15 "launchUrl": "api/values", //在浏览器中启动的相对URL
16 "environmentVariables": { //将环境变量设置为键/值对
17 "ASPNETCORE_ENVIRONMENT": "Development"
18 }
19 },
20 "NetCoreWebApi": {
21 "commandName": "Project",
22 "launchBrowser": true,
23 "launchUrl": "api/values",
24 "applicationUrl": "http://localhost:5000",
25 "environmentVariables": {
26 "ASPNETCORE_ENVIRONMENT": "Development"
27 }
28 }
29 }
30 }
appsetting.json
appsetting.json是应用程序配置文件,类似于ASP.NET MVC应用程序中的Web.config配置文件。
例如:连接字符串,等等....
1 {
2 "Logging":{
3 "LogLevel":{
4 "Default":"Warning"
5 }
6 },
7 "AllowedHosts":"*",
8 "ConnectionStrings":{
9 "SqlServer":"Data Source=.;Initial Catalog=NetCoreWebApi;User Id=sa;Password=123;"
10 }
11 }
Program.cs
Program.cs文件是应用的入口, 启动后通过UseStartup<Startup>()指定下文的Startup启动文件进行启动。
1 public class Program
2 {
3 public static void Main(string[] args)
4 {
5 CreateWebHostBuilder(args).Build().Run();
6 }
7
8 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
9 WebHost.CreateDefaultBuilder(args)//创建WebHostBuilder。
10 .UseStartup<Startup>();//指定启动类,用于依赖注入和中间件注册。
11 }
Startup.cs
该文件是默认文件,不可随意删除,在此文件中可以以包含服务配置、定义请求处理管道的重要操作。
ConfigureServices 方法用于将服务注入到 IServiceCollection 服务容器中。
Configure方法用于应用响应 HTTP 请求,将中间件注册到 ApplicationBuilder中来配置请求管道。
1 public class Startup
2 {
3 public Startup(IConfiguration configuration)
4 {
5 Configuration = configuration;
6 }
7
8 public IConfiguration Configuration { get; }
9
10 //负责注入服务
11 public void ConfigureServices(IServiceCollection services)
12 {
13 services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
14 }
15
16 // 响应 HTTP 请求的方式。可将中间件注册到IApplicationBuilder 实例来配置请求管道。
17 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
18 {
19 if (env.IsDevelopment())
20 {
21 app.UseDeveloperExceptionPage();
22 }
23
24 app.UseMvc();
25 }
26 }
三、测试
此时项目已经完成,按 Ctrl+F5 运行之后,就能看到
这是一个最基础的.NET Core API 环境,离我们想要的API框架还很远,但是其实已经成功一半了,因为好的开始是成功的一半!
关于asp.net-core – 如何在我的ASP.NET核心WebApi项目中全局启用CORS和asp.net core 3全栈web开发的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net-core – 如何创建我可以从.NET核心应用程序(Web API)引用的.NET核心库、.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置、.NET6-Asp.Net Core webapi -从零开始的webapi项目、ASP.NET Core 2.2 WebApi 系列【一】搭建ASP.NET Core WebApi项目等相关知识的信息别忘了在本站进行查找喔。
本文标签: