将表单POST HTTP request(Content-Type: application/x-www-form-urlencoded)发送到以下控制器会导致HTTP 415 Unsupported Media Type响应.
public class MyController : Controller
{
[HttpPost]
public async Task<IActionResult> Submit([FromBody] MyModel model)
{
//...
}
}
Run Code Online (Sandbox Code Playgroud)
表单发布HTTP标头:
POST /submit HTTP/1.1
Host: example.com:1337
Connection: keep-alive
Content-Length: 219
Pragma: no-cache
Cache-Control: no-cache
Origin: https://example.com:1337
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Referer: https://example.com:1337/submit
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8,nl;q=0.6
Run Code Online (Sandbox Code Playgroud)
这曾经用于.NET 4.6上的ASP.NET MVC 5.
在appsettings.json中
{
"MyArray": [
"str1",
"str2",
"str3"
]
}
Run Code Online (Sandbox Code Playgroud)
在Startup.cs中
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConfiguration>(Configuration);
}
Run Code Online (Sandbox Code Playgroud)
在HomeController中
public class HomeController : Controller
{
private readonly IConfiguration _config;
public HomeController(IConfiguration config)
{
this._config = config;
}
public IActionResult Index()
{
return Json(_config.GetSection("MyArray"));
}
}
Run Code Online (Sandbox Code Playgroud)
上面有我的代码,我得到null如何获取数组?
不知道我在这里缺少什么,但我无法从我的.net核心应用程序中的appsettings.json获取值.我的appsettings.json为:
{
"AppSettings": {
"Version": "One"
}
}
Run Code Online (Sandbox Code Playgroud)
启动:
public class Startup
{
private IConfigurationRoot _configuration;
public Startup(IHostingEnvironment env)
{
_configuration = new ConfigurationBuilder()
}
public void ConfigureServices(IServiceCollection services)
{
//Here I setup to read appsettings
services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
}
}
Run Code Online (Sandbox Code Playgroud)
模型:
public class AppSettings
{
public string Version{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public class HomeController : Controller
{
private readonly AppSettings _mySettings;
public HomeController(IOptions<AppSettings> settings)
{
//This is always null
_mySettings = settings.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
_mySettings永远是空的.这里有什么我想念的吗?
阅读这两个问题/答案,我能够在IIS 8.5服务器上运行Asp.net 5应用程序.
Asp.net vNext早期测试版在Windows服务器上发布到IIS
问题是,即使在IIS上运行,Web应用程序仍然使用env.EnvironmentName值Development.
另外,我想在同一台服务器上运行同一个Web(Staging,Production)的两个版本,所以我需要一个方法来分别为每个Web设置变量.
这该怎么做?
每当我向我的asp.net核心项目添加javascript或css文件并dotnet run在我的bash终端中执行时,我都会收到以下错误:
/usr/share/dotnet/sdk/1.0.1/Sdks/Microsoft.NET.Sdk/build/Microsoft
.NET.Sdk.DefaultItems.targets(188,5):错误:包含重复的"内容"项..NET SDK默认包含项目目录中的"内容"项.您可以从项目文件中删除这些项目,或者如果要在项目文件中明确包含它们,请将"EnableDefaultContentItems"属性设置为"false".有关更多信息,请参阅https://aka.ms/sdkimplicititems.重复的项目是:'wwwroot/css/BasicQuotation.css'; 'wwwroot/js/BasicQuotation.js'[/mnt/c/Dev/myproject/MyProject/MyProject.csproj]
构建失败.请修复构建错误并再次运行.
我可以通过删除ItemGroup我的csproj文件来解决这个问题,但我认为这不是很有效率.
这在默认的Visual Studio 2017 ASP.NET核心Web应用程序(.NET Core)模板中发生.我通过右键单击wwwroot> js文件夹将文件添加到我的项目中,然后选择Add > New Item > JavaScript File
这是我的.csproj文件:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<PropertyGroup>
<PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>
<PropertyGroup>
<UserSecretsId>aspnet-MyProject-7e1906d8-5dbd-469a-b237-d7a563081253</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Compile Remove="wwwroot\lib\jquery-validation\**" />
<Content Remove="wwwroot\lib\jquery-validation\**" />
<EmbeddedResource Remove="wwwroot\lib\jquery-validation\**" />
<None Remove="wwwroot\lib\jquery-validation\**" />
</ItemGroup>
<ItemGroup>
<Content Include="wwwroot\css\BasicQuotation.css" />
<Content Include="wwwroot\js\BasicQuotation.js" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Cookies" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" …Run Code Online (Sandbox Code Playgroud) 我希望下面的示例控制器返回没有内容的状态代码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) HttpContext.Current.Server.UrlEncode
Run Code Online (Sandbox Code Playgroud)
它仅适用于.NET Framework.如何在ASP.NET Core项目中编码或解码uri参数?
在以前的asp.net版本中,我们可以使用
@Request.Url.AbsoluteUri
Run Code Online (Sandbox Code Playgroud)
但它似乎已经改变了.我们怎样才能在asp.net core 1.0中做到这一点?
升级到ASP.NET Core 2.0后,我似乎无法再创建迁移.
我越来越
"在类'Program'上调用方法'BuildWebHost'时发生错误.继续没有应用服务提供者.错误:发生了一个或多个错误.(无法打开登录请求的数据库"...".登录失败.登录用户'...'失败
和
"无法创建'MyContext'类型的对象.将'IDesignTimeDbContextFactory'的实现添加到项目中,或者参见 https://go.microsoft.com/fwlink/?linkid=851728以获取在设计时支持的其他模式."
我之前运行的命令是$ dotnet ef migrations add InitialCreate --startup-project "..\Web"(来自带有DBContext的项目/文件夹).
连接字符串: "Server=(localdb)\\mssqllocaldb;Database=database;Trusted_Connection=True;MultipleActiveResultSets=true"
这是我的Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
Run Code Online (Sandbox Code Playgroud) c# entity-framework-core asp.net-core-mvc asp.net-core asp.net-core-2.0