小编Hen*_*ema的帖子

使用ASP.NET Core DI解析实例

如何使用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.

c# dependency-injection asp.net-core-mvc asp.net-core

245
推荐指数
6
解决办法
17万
查看次数

如何在ASP.NET Core中启用CORS

我试图在我的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

c# asp.net-core

154
推荐指数
10
解决办法
11万
查看次数

System.Net.Http与Microsoft.Net.Http

我正在使用ASP.NET Core.我想使用,HttpClient但我注意到有两个NuGet包提供.我用哪一个?

c# httpclient nuget asp.net-core-mvc asp.net-core

74
推荐指数
3
解决办法
2万
查看次数

如何在ASP.NET Core MVC中获取Url Referrer?

我正在尝试将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)

如果有人能指引我朝着正确的方向前进,我真的很感激.

c# asp.net-core-mvc asp.net-core

72
推荐指数
5
解决办法
4万
查看次数

如何在ASP.NET Core中解析ConfigureServices中的实例

是否可以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)

c# inversion-of-control asp.net-core-mvc asp.net-core

67
推荐指数
3
解决办法
3万
查看次数

在ASP.NET Core中的Program.Main中访问环境名称

使用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)

但似乎没有办法从命令行使用证书.

c# kestrel asp.net-core

51
推荐指数
3
解决办法
2万
查看次数

从IEnumerable <T>中删除项目

我有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)

但它不起作用.方法执行后的计数表示没有删除任何项目.

c# linq ienumerable

45
推荐指数
4
解决办法
8万
查看次数

如何在ASP.NET Core MVC中启用跨源请求(CORS)

我想在使用ASP.NET Core MVC构建的API上启用CORS,但所有当前文档都引用该框架的早期版本.

c# cors asp.net-core-mvc asp.net-core

33
推荐指数
3
解决办法
2万
查看次数

ASP.NET 5中的RoutePrefixAttribute

我在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)

我应该使用其他课吗?

c# asp.net-web-api2 dnx asp.net-core

17
推荐指数
1
解决办法
9663
查看次数

mvc 5检查用户角色

如何在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# authentication asp.net-mvc-5 asp.net-identity

16
推荐指数
1
解决办法
4万
查看次数