如何在Core 2.0中的ConfigurationBuilder中设置基本路径.
我用Google搜索,发现这个问题,这个来自微软的文档,以及2.0文档在线,但他们似乎使用的版本Microsoft.Extension.Configuration从1.0.0-beta8.
我想读appsettings.json.在Core 2.0中有没有新的方法?
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
namespace ConsoleApp2
{
class Program
{
public static IConfigurationRoot Configuration { get; set; }
static void Main(string[] args)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory()) // <== compile failing here
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
Console.WriteLine(Configuration.GetConnectionString("con"));
Console.WriteLine("Press a key...");
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
appsetting.json
{
"ConnectionStrings": {
"con": "connection string"
}
}
Run Code Online (Sandbox Code Playgroud)
更新: 除了添加Microsoft.Extensions.Configuration.FileExtensions如下图所示的设置我还需要添加 …
我想发送动态对象
new { x = 1, y = 2 };
Run Code Online (Sandbox Code Playgroud)
作为HTTP POST消息的主体.所以我试着写
var client = new HttpClient();
Run Code Online (Sandbox Code Playgroud)
但我找不到方法
client.PostAsJsonAsync()
Run Code Online (Sandbox Code Playgroud)
所以我尝试将Microsoft.AspNetCore.Http.Extensions包添加到project.json并添加
using Microsoft.AspNetCore.Http.Extensions;
Run Code Online (Sandbox Code Playgroud)
使用条款.但它没有帮助我.
那么在ASP.NET Core中使用JSON主体发送POST请求的最简单方法是什么?
我在 .NET 5 中创建了 REST API,一切都运行良好,但最近我转移到 .NET 6 并意识到不存在startup.cs 类。由于没有startup.cs,如何在.NET 6 中添加数据库上下文?
我正在使用npm来管理我的ASP.NET核心应用程序所需的jQuery,Bootstrap,Font Awesome和类似的客户端库.
对我有用的方法首先将package.json文件添加到项目中,如下所示:
{
"version": "1.0.0",
"name": "myapp",
"private": true,
"devDependencies": {
},
"dependencies": {
"bootstrap": "^3.3.6",
"font-awesome": "^4.6.1",
"jquery": "^2.2.3"
}
}
Run Code Online (Sandbox Code Playgroud)
npm将这些包恢复到node_modules文件夹中,该文件夹与项目目录中的wwwroot处于同一级别:
由于ASP.NET Core提供了来自wwwroot文件夹的静态文件,并且node_modules不在那里,我不得不进行一些更改才能完成这项工作,第一个:在app.UseStaticFiles之前添加app.UseFileServer. cs文件:
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"node_modules")),
RequestPath = new PathString("/node_modules"),
EnableDirectoryBrowsing = true
});
app.UseStaticFiles();
Run Code Online (Sandbox Code Playgroud)
第二个,包括project.json文件中的publishOptions中的node_modules:
"publishOptions": {
"include": [
"web.config",
"wwwroot",
"Views",
"node_modules"
]
},
Run Code Online (Sandbox Code Playgroud)
这适用于我的开发环境,当我将它部署到我的Azure App Service实例时,它也可以工作,jquery,bootstrap和font-awesome静态文件得到很好的服务,但我不确定这个实现.
这样做的正确方法是什么?
这个解决方案是在从多个来源收集大量信息并尝试一些不起作用之后得出的,并且从wwwroot外部提供这些文件似乎有点奇怪.
任何建议将不胜感激.
我希望下面的示例控制器返回没有内容的状态代码418.设置状态代码很容易,但似乎需要做一些事情来发出请求结束的信号.在ASP.NET Core之前的MVC或WebForms中可能是一个调用,Response.End()但它如何在ASP.NET Core中Response.End不存在?
public class ExampleController : Controller
{
[HttpGet][Route("/example/main")]
public IActionResult Main()
{
this.HttpContext.Response.StatusCode = 418; // I'm a teapot
// How to end the request?
// I don't actually want to return a view but perhaps the next
// line is required anyway?
return View();
}
}
Run Code Online (Sandbox Code Playgroud) 我已经完成了ASP.NET核心的配置文档.文档说您可以从应用程序的任何位置访问配置.
下面是模板创建的Startup.cs
public class Startup
{
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container
public void ConfigureServices(IServiceCollection …Run Code Online (Sandbox Code Playgroud) 我觉得我错过了一些非常明显的东西.我有类需要使用.Net Core IOptions模式注入选项(?).当我去单元测试那个类时,我想模拟各种版本的选项来验证类的功能.有谁知道如何正确模拟/实例化/填充Startup类之外的IOptions?
以下是我正在使用的类的一些示例:
设置/选项模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace OptionsSample.Models
{
public class SampleOptions
{
public string FirstSetting { get; set; }
public int SecondSetting { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
要使用设置测试的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OptionsSample.Models
using System.Net.Http;
using Microsoft.Extensions.Options;
using System.IO;
using Microsoft.AspNetCore.Http;
using System.Xml.Linq;
using Newtonsoft.Json;
using System.Dynamic;
using Microsoft.Extensions.Logging;
namespace OptionsSample.Repositories
{
public class SampleRepo : ISampleRepo
{
private SampleOptions _options;
private ILogger<AzureStorageQueuePassthru> _logger;
public SampleRepo(IOptions<SampleOptions> …Run Code Online (Sandbox Code Playgroud) 我正在使用ASP.NET Core MVC构建一个RESTful API,我想使用查询字符串参数来指定返回集合的资源上的过滤和分页.
在这种情况下,我需要读取查询字符串中传递的值来过滤并选择要返回的结果.
我已经发现控制器Get操作内部访问HttpContext.Request.Query返回一个IQueryCollection.
问题是我不知道如何使用它来检索值.事实上,我认为要做的方法是使用,例如
string page = HttpContext.Request.Query["page"]
Run Code Online (Sandbox Code Playgroud)
问题是HttpContext.Request.Query["page"]不返回字符串,而是返回StringValues.
无论如何,如何使用它IQueryCollection来实际读取查询字符串值?
我在尝试启动应用程序时收到此错误消息.
尝试确定托管应用程序的DNX进程的进程ID时发生错误
有没有办法解决这个问题?
我想让当前用户获取用户的信息,例如电子邮件.但我不能在asp.net核心中这样做.我很困惑这是我的代码.
HttpContext在控制器的构造函数中几乎为null .在每个动作中获得用户并不好.我想获得用户的信息并将其设置为ViewData;
public DashboardController()
{
var user = HttpContext.User.GetUserId();
}
Run Code Online (Sandbox Code Playgroud) asp.net-core ×10
c# ×9
.net-core ×3
.net ×1
.net-6.0 ×1
asp.net-mvc ×1
gulp ×1
http ×1
npm ×1
query-string ×1
unit-testing ×1
webapi ×1