我正在尝试在ASP.NET Core中创建自定义授权属性.在以前的版本中,可以覆盖bool AuthorizeCore(HttpContextBase httpContext).但这不再存在于AuthorizeAttribute.
制作自定义AuthorizeAttribute的当前方法是什么?
我想要完成的任务:我在Header Authorization中收到一个会话ID.从该ID我将知道特定动作是否有效.
如何使用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.
我有来自相同接口的服务
public interface IService { }
public class ServiceA : IService { }
public class ServiceB : IService { }
public class ServiceC : IService { }
Run Code Online (Sandbox Code Playgroud)
通常,其他IOC容器Unity允许您通过某些Key区分它们来注册具体实现.
在Asp.Net Core中,我如何注册这些服务并在运行时根据某些键解决它?
我没有看到任何通常用于区分具体实现的AddService方法key或name参数.
public void ConfigureServices(IServiceCollection services)
{
// How do I register services here of the same interface
}
public MyController:Controller
{
public void DoSomeThing(string key)
{
// How do get service based on key
}
}
Run Code Online (Sandbox Code Playgroud)
工厂模式是唯一的选择吗?
Update1
我已经阅读了这里 …
我试图从Angular调用API但是我收到此错误:
Property 'map' does not exist on type 'Observable<Response>'
这个类似问题的答案并没有解决我的问题:Angular 2 beta.17:'Observable <Response>'类型中不存在属性'map'.
我正在使用Angular 2.0.0-beta.17.
你能否告诉我如何在使用MVC 6时在ASP.NET中获取客户端IP地址Request.ServerVariables["REMOTE_ADDR"]不起作用.
回到RC1,我会这样做:
[HttpPost]
public IActionResult Post([FromBody]string something)
{
try{
// ...
}
catch(Exception e)
{
return new HttpStatusCodeResult((int)HttpStatusCode.InternalServerError);
}
}
Run Code Online (Sandbox Code Playgroud)
在RC2中,不再有HttpStatusCodeResult,而且我找不到任何东西可以让我返回500类型的IActionResult.
对于我要问的方法,这种方法现在完全不同吗?我们不再尝试捕获Controller代码吗?我们只是让框架将一个通用500异常抛回API调用者吗?对于开发,我怎样才能看到确切的异常堆栈?
我创建了一个.NET Core MVC应用程序,并使用依赖注入和存储库模式将一个存储库注入我的控制器.但是,我收到一个错误:
InvalidOperationException:尝试激活"WebApplication1.Controllers.BlogController"时,无法解析类型"WebApplication1.Data.BloggerRepository"的服务.
型号(Blog.cs)
namespace WebApplication1.Models
{
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
DbContext(BloggingContext.cs)
using Microsoft.EntityFrameworkCore;
using WebApplication1.Models;
namespace WebApplication1.Data
{
public class BloggingContext : DbContext
{
public BloggingContext(DbContextOptions<BloggingContext> options)
: base(options)
{ }
public DbSet<Blog> Blogs { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
存储库(IBloggerRepository.cs和BloggerRepository.cs)
using System;
using System.Collections.Generic;
using WebApplication1.Models;
namespace WebApplication1.Data
{
internal interface IBloggerRepository : IDisposable
{
IEnumerable<Blog> GetBlogs();
void InsertBlog(Blog blog);
void …Run Code Online (Sandbox Code Playgroud) 我需要帮助ASP.NET Core中的select标签助手.
我有一个员工列表,我正在尝试绑定到选择标记帮助程序.我的员工在一个List<Employee> EmployeesList和选定的价值将进入EmployeeId财产.我的视图模型如下所示:
public class MyViewModel
{
public int EmployeeId { get; set; }
public string Comments { get; set; }
public List<Employee> EmployeesList {get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的员工类看起来像这样:
public class Employee
{
public int Id { get; set; }
public string FullName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何告诉我的选择标记助手在下拉列表中Id显示时使用该值作为值FullName?
<select asp-for="EmployeeId" asp-items="???" />
Run Code Online (Sandbox Code Playgroud)
我很感激这方面的一些帮助.谢谢.
在使用MVC5之前我已经完成了这个,User.Identity.GetUserId()但这似乎不适用于此.本User.Identity可是没有的GetUserId()方法
我在用 Microsoft.AspNet.Identity
如何ConfigureServices在Startup 中的方法中获得开发/暂存/生产主机环境?
public void ConfigureServices(IServiceCollection services)
{
// Which environment are we running under?
}
Run Code Online (Sandbox Code Playgroud)
该ConfigureServices方法仅采用单个IServiceCollection参数.