在RC1中,我会这样做:

[HttpPost]
public IActionResult Post([FromBody]string something)
{    
    try{
        // ...
    }
    catch(Exception e)
    {
         return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
    }
}

在RC2中,不再有HttpStatusCodeResult,也没有什么我可以找到,让我返回一个500类型的IActionResult。

现在的方法与我要求的完全不同吗?我们在控制器代码中不再尝试捕获了吗?我们只是让框架向API调用者抛出一个通用的500异常吗?对于开发,我如何才能看到确切的异常堆栈?

发送一个表单POST HTTP请求(Content-Type: application/x-www-form-urlencoded)到下面的控制器,结果是一个HTTP 415不支持的媒体类型响应。

public class MyController : Controller
{
    [HttpPost]
    public async Task<IActionResult> Submit([FromBody] MyModel model)
    {
        //...
    }
}

表单post 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

这在过去是与ASP一起工作的。NET MVC 5在。NET 4.6。

不确定我在这里错过了什么,但我无法从我的应用程序设置中获得值。Json在我的。net核心应用程序。我有我的appsettings。json:

{
    "AppSettings": {
        "Version": "One"
    }
}

启动:

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"));
    }
}

模型:

public class AppSettings
{
    public string Version{ get; set; }
}

控制器:

public class HomeController : Controller
{
    private readonly AppSettings _mySettings;

    public HomeController(IOptions<AppSettings> settings)
    {
        //This is always null
        _mySettings = settings.Value;
    }
}

_mySettings总是空的。我是不是遗漏了什么?

如何在启动中的ConfigureServices方法中获得开发/登台/生产托管环境?

public void ConfigureServices(IServiceCollection services)
{
    // Which environment are we running under?
}

ConfigureServices方法只接受一个IServiceCollection参数。

在appsettings.json

{
      "MyArray": [
          "str1",
          "str2",
          "str3"
      ]
}

在Startup.cs

public void ConfigureServices(IServiceCollection services)
{
     services.AddSingleton<IConfiguration>(Configuration);
}

在HomeController

public class HomeController : Controller
{
    private readonly IConfiguration _config;
    public HomeController(IConfiguration config)
    {
        this._config = config;
    }
    
    public IActionResult Index()
    {
        return Json(_config.GetSection("MyArray"));
    }
}

上面是我的代码。结果是零。 如何获取数组?

请问如何在ASP中获取客户端IP地址?NET时使用MVC 6。 请求。ServerVariables["REMOTE_ADDR"]无效。

我创建了一个。net核心MVC应用程序,并使用依赖注入和存储库模式将存储库注入到我的控制器。然而,我得到一个错误:

无法解析类型为“WebApplication1.Data”的服务。当试图激活“WebApplication1.Controllers.BlogController”。

存储库:

public interface IBloggerRepository { ... }

public class BloggerRepository : IBloggerRepository { ... }

控制器:

public class BlogController : Controller
{
    private readonly IBloggerRepository _repository;

    public BlogController(BloggerRepository repository)
    {
        _repository = repository;
    }

    public IActionResult Index() { ... }
}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    
    services.AddScoped<IBloggerRepository, BloggerRepository>();
}

我不知道我哪里做错了。什么好主意吗?

我拥有从同一接口派生的服务。

public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { } 
public class ServiceC : IService { }

通常情况下,其他IoC容器如Unity允许你通过一些区分它们的Key来注册具体的实现。

在ASP。NET Core,我如何注册这些服务,并在运行时根据一些关键字解析它们?

我没有看到任何接受键或名称参数的Add Service方法,这些参数通常用于区分具体实现。

    public void ConfigureServices(IServiceCollection services)
    {            
         // How do I register services of the same interface?            
    }


    public MyController:Controller
    {
       public void DoSomething(string key)
       { 
          // How do I resolve the service by key?
       }
    }

工厂模式是这里唯一的选择吗?

Update1 在这篇文章中,我已经展示了当我们有多个具体实现时,如何使用工厂模式来获取服务实例。然而,这仍然不是一个完整的解决方案。当我调用_serviceProvider.GetService()方法时,我不能将数据注入构造函数。

举个例子:

public class ServiceA : IService
{
     private string _efConnectionString;
     ServiceA(string efconnectionString)
     {
       _efConnecttionString = efConnectionString;
     } 
}

public class ServiceB : IService
{    
   private string _mongoConnectionString;
   public ServiceB(string mongoConnectionString)
   {
      _mongoConnectionString = mongoConnectionString;
   }
}

public class ServiceC : IService
{    
    private string _someOtherConnectionString
    public ServiceC(string someOtherConnectionString)
    {
      _someOtherConnectionString = someOtherConnectionString;
    }
}

_serviceProvider.GetService()如何注入适当的连接字符串? 在Unity或任何其他IoC库中,我们可以在类型注册时做到这一点。我可以使用IOption,但是,这将要求我注入所有设置。我无法向服务中注入特定的连接字符串。

还要注意,我试图避免使用其他容器(包括Unity),因为这样我就必须注册其他所有东西(例如,控制器)与新容器以及。

此外,使用工厂模式创建服务实例是不利于DIP的,因为它增加了客户端在这里有详细信息的依赖项数量。

所以,我认为默认DI在ASP。NET Core缺少两样东西:

使用键注册实例的能力 在注册期间向构造函数注入静态数据的能力

我如何手动解析一个类型使用ASP。NET核心MVC内置依赖注入框架?

设置容器非常简单:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddTransient<ISomeService, SomeConcreteService>();
}

但是如何在不执行注入的情况下解析ISomeService呢?例如,我想这样做:

ISomeService service = services.Resolve<ISomeService>();

在IServiceCollection中没有这样的方法。

我试图使一个自定义授权属性在ASP。净的核心。在以前的版本中,可以重写bool AuthorizeCore(HttpContextBase httpContext)。但是这在AuthorizeAttribute中不再存在。

当前制作自定义AuthorizeAttribute的方法是什么?

我想要完成的:我正在头授权中接收会话ID。通过该ID,我将知道特定操作是否有效。