如何使用ASP.NET Core MVC内置依赖注入框架手动解析类型?
设置容器很容易:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddTransient<ISomeService, SomeConcreteService>();
}
Run Code Online (Sandbox Code Playgroud)
但是如何在ISomeService不进行注射的情况下解决?例如,我想这样做:
ISomeService service = services.Resolve<ISomeService>();
Run Code Online (Sandbox Code Playgroud)
没有这样的方法IServiceCollection.
我试图在我的ASP.NET Core Web API上启用跨源资源共享,但我被困住了.
该EnableCors属性接受policyName类型string为参数:
// Summary:
// Creates a new instance of the Microsoft.AspNetCore.Cors.Core.EnableCorsAttribute.
//
// Parameters:
// policyName:
// The name of the policy to be applied.
public EnableCorsAttribute(string policyName);
Run Code Online (Sandbox Code Playgroud)
这policyName意味着什么以及如何在ASP.NET Core Web API上配置CORS?
我正在使用ASP.NET Core.我想使用,HttpClient但我注意到有两个NuGet包提供.我用哪一个?
我正在尝试将ASP.NET MVC webform迁移到ASP.NET Core MVC.目前,我在Request.UrlReferrer上课时遇到了麻烦.
原来的行是:
[HttpPost]
public async Task<ActionResult> ContactUsFormSubmit(ContactUs request)
{
var siteUrl = Request.UrlReferrer.ToString().ToLower();
....
}
Run Code Online (Sandbox Code Playgroud)
但是,使用ASP.NET Core时,UrlReferrer不可用.我发现了以下内容:
Request.Headers["Referer"]
Run Code Online (Sandbox Code Playgroud)
返回StringValues而不是String.我不确定我是否应该尝试使用这个或者是否有任何其他解决方案.Request.ServerVariables也不可用或者我没有命名空间.我的命名空间如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Run Code Online (Sandbox Code Playgroud)
如果有人能指引我朝着正确的方向前进,我真的很感激.
是否可以IOptions<AppSettings>从ConfigureServicesStartup中的方法解析实例?通常,您可以使用IServiceProvider初始化实例,但在注册服务时此阶段没有实例.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppSettings>(
configuration.GetConfigurationSection(nameof(AppSettings)));
// How can I resolve IOptions<AppSettings> here?
}
Run Code Online (Sandbox Code Playgroud) 使用ASP.NET Mvc Core我需要将我的开发环境设置为使用https,因此我将以下内容添加到MainProgram.cs中的方法:
var host = new WebHostBuilder()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseKestrel(cfg => cfg.UseHttps("ssl-dev.pfx", "Password"))
.UseUrls("https://localhost:5000")
.UseApplicationInsights()
.Build();
host.Run();
Run Code Online (Sandbox Code Playgroud)
如何在此处访问托管环境,以便我可以有条件地设置协议/端口号/证书?
理想情况下,我会使用CLI来操纵我的托管环境,如下所示:
dotnet run --server.urls https://localhost:5000 --cert ssl-dev.pfx password
Run Code Online (Sandbox Code Playgroud)
但似乎没有办法从命令行使用证书.
我有2个IEnumerable集合.
IEnumerable<MyClass> objectsToExcept
Run Code Online (Sandbox Code Playgroud)
和
IEnumerable<MyClass> allObjects.
Run Code Online (Sandbox Code Playgroud)
objectsToExcept可能包含来自的对象allObjects.
我需要从中删除allObjects对象objectsToExcept.例如:
foreach (var myClass in objectsToExcept)
{
allObjects.Remove(myClass);
}
Run Code Online (Sandbox Code Playgroud)
要么
allObject.Except(objectsToExcept)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.方法执行后的计数表示没有删除任何项目.
我想在使用ASP.NET Core MVC构建的API上启用CORS,但所有当前文档都引用该框架的早期版本.
我在ASP.NET 5中启动了一个新的Web API 2.0项目.我尝试创建自定义RoutePrefixAttribute类,但是我收到此错误
The type or namespace name 'RoutePrefixAttribute' could not be found
(are you missing a using directive or an assembly reference?) {ProjectName}.DNX Core 5.0
Run Code Online (Sandbox Code Playgroud)
我应该使用其他课吗?
如何在mvc 5中找到登录用户的角色?
我通过此代码创建了用户
private bool AddUserAndRole()
{
IdentityResult ir;
var rm = new RoleManager<IdentityRole>
(new RoleStore<IdentityRole>(new ApplicationDbContext()));
ir = rm.Create(new IdentityRole("admin"));
var user = new ApplicationUser() { UserName = "Admin" };
var result = UserManager.Create(user, "somepassword");
UserManager.AddToRole(user.Id, "admin");
return true;
}
Run Code Online (Sandbox Code Playgroud)
我在该网站上登录之后.如何在控制器中检查该用户是否具有角色=="admin"?我发现只有一种看起来不那么快的方法.
var rm = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var role = rm.FindByName("admin");
bool result = User.IsInRole(role.Name); //true
Run Code Online (Sandbox Code Playgroud)
我们还有其他方法吗?
c# ×10
asp.net-core ×8
cors ×1
dnx ×1
httpclient ×1
ienumerable ×1
kestrel ×1
linq ×1
nuget ×1