Tag: asp.net core

Asp.net Core Post参数始终为null

我从小提琴手发送POST: POST http://localhost:55924/api/Product HTTP/1.1 User-Agent: Fiddler Host: localhost:55924 Content-Type: application/json; charset=utf-8 Content-Length: 84 {“Ean”:”1122u88991″,”Name”:”Post test”,”Description”:”Post test desc”} 但Post方法总是为空。 // POST api/Product [HttpPost] public IActionResult PostProduct([FromBody]Product product) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _repo.Add(product); return CreatedAtRoute(“GetToode”,product); } 当我使用[FormBody]时,产品始终为null,当不使用时,产品会被重视,但所有字段都为null。 产品类很简单。 public class Product { public int ProductID { get; set; } public string EAN { get; […]

ASP.NET CORE 1.0,模拟

我正在写一个Intranet应用程序。 project.json中的目标框架是dnx451。 这是我的发布命令: dnu publish –runtime dnx-clr-win-x86.1.0.0-rc1-update1 –no-source Database Connection字符串: Server=name;Database=name;Trusted_Connection=True; 我正在尝试模拟数据库访问,但它无法正常工作。 当我启动应用程序时,我的Windows用户被识别,它在右上角显示Hello,Domain \ Username。 一旦我尝试访问数据库,我就会收到错误“登录用户域\ Computername失败”。 如果我在我的用户下运行应用程序池,那么一切正常。 IIS:.NET CLR Versio是v4.0,托管Pipline模式Classic和Identity是ApplicationPoolIdentity。 网站身份validation:启用了ASP.NET模拟和Windows身份validation。 我需要做些什么才能改变模仿终于有效?

如何添加通用dependency injection

使用只读api服务并利用generics将操作打包到基于约定的过程中。 存储库界面: public interface IRepository where TEntityType:class { Task<EntityMetadata> GetMetaAsync(); } 存储库实现: public class Repository : IRepository where TEntityType:class { public Repository(string connectionString) { // initialization } public async Tas<EntityMetadata> GetMetaAsync() { // implementation } } 在Startup.cs -> ConfigureServices : services.AddSingleton<IRepository> ( p=> new Repository(connectionString)); services.AddSingleton<IRepository> ( p=> new Repository(connectionString)); // and so on 控制器: […]

如何从控制台应用程序中的config.json读取值

我刚刚安装了ASP.NET 5并在Visual Studio中创建了一个控制台应用程序。 我已经将一个文件config.json添加到项目的根文件夹中。 它看起来像这样: { “Data”: { “TargetFolderLocations”: { “TestFolder1”: “Some path”, “TestFolder2”: “Another path” } } } 我的Program.cs看起来像这样 public void Main(string[] args) { var configurationBuilder = new ConfigurationBuilder(Environment.CurrentDirectory) .AddJsonFile(“config.json”) .AddEnvironmentVariables(); Configuration = configurationBuilder.Build(); //Doesn’t work…null all the time var test = Configuration.Get(“Data:TargetFolderLocations”); Console.ReadLine(); } 如何使用代码访问TargetFolderLocations键?

ASP.NET 5,MVC6,WebAPI – > ModelState.IsValid始终返回true

我已经看到很多关于IsValid的post总是正确的,但没有一个能帮助我解决这个问题。 我也在使用MVC5的ASP.NET 4中看到了这个问题。 很明显,我在某个地方错过了一步。 控制器方法: public IHttpActionResult Post([FromBody]ValuesObject value) { if (ModelState.IsValid) { return Json(value); } else { return Json(ModelState); } } ValuesObject类: public class ValuesObject { [Required] public string Name; [Range(10, 100, ErrorMessage = “This isn’t right”)] public int Age; } POST的身体: { Age: 1 } ModelState.IsValid为true。 但我希望Required和Rangevalidation都失败。 我错过了什么? 谢谢, 凯文

必须提供“SignInScheme”选项

我正在创建一个仅使用Facebook / Google身份validation的ASP.NET 5 MVC 6应用程序。 我也试图在没有整个ASP.NET身份的情况下使用cookie中间件 – 遵循这篇文章: https : //docs.asp.net/en/latest/security/authentication/cookie.html 所以我开始使用没有身份validation的空白应用程序然后添加了Microsoft.AspNet.Authentication.Cookies和Microsoft.AspNet.Authentication.Facebook NuGet包,以便采用非常简约的方法,其中我不包含任何我不包含的内容需要。 我将以下代码添加到Startup.cs中的Configure中,但我得到“必须提供SignInScheme选项”错误。 知道我错过了什么吗? app.UseCookieAuthentication(options => { options.AuthenticationScheme = “MyCookieMiddlewareInstance”; options.LoginPath = new PathString(“/Accounts/Login/”); options.AccessDeniedPath = new PathString(“/Error/Unauthorized/”); options.AutomaticAuthenticate = true; options.AutomaticChallenge = true; }); app.UseFacebookAuthentication(options => { options.AppId = “myFacebookAppIdGoesHere”; options.AppSecret = “myFacebookAppSecretGoesHere”; });

如何注册通用服务

我打算将我的系统移动到通用服务层。 public interface IAbcService where TEntity : class public class AbcService : IAbcService where TEntity : class 我尝试了一些东西,但它不起作用。 services.AddTransient<typeof(IAbcService), AbcService(); …. …. …. 我正在使用我的一些实体,并且我正在尝试将其添加到属性中。 如何在asp.net核心中注册通用服务?

用户授权失败:(null)

我正在使用ASP.NET MVC Core的自定义身份validation,它不使用Identity 。 这是Startup.cs : public class Startup { public IConfiguration Configuration { get; set; } // Configure IoC container // https://docs.asp.net/en/latest/fundamentals/dependency-injection.html public void ConfigureServices(IServiceCollection services) { services.Configure(options => Configuration.GetSection(nameof(AppSettings)).Bind(options)); // https://docs.asp.net/en/latest/security/anti-request-forgery.html services.AddAntiforgery(options => options.CookieName = options.HeaderName = “X-XSRF-TOKEN”); services.AddDbContext(options => { options.UseSqlServer(Configuration.GetConnectionString(“SesamConnection”)); }); services.AddDbContext(options => { options.UseSqlServer(Configuration.GetConnectionString(“TerminalConnection”)); }); services.AddMvcCore() .AddAuthorization() .AddViews() .AddRazorViewEngine() .AddJsonFormatters(); […]

.NET MVC 6 / vNext UserValidator允许使用字母数字字符

我想要做的只是在.NET MVC 6 / vNext注册过程中允许使用字母数字字符,并且我看到的每个教程(即配置Microsoft.AspNet.Identity允许电子邮件地址作为用户名 )引用UserManager.UserValidator ,in现在在当前框架中不可用。 我已经看到了这一点: Microsoft.AspNet.Identity vnext中的UserValidator看起来与我的问题相同,但是他们从那时起就将UserNameValidationRegex属性从框架中删除了。 辉煌。 services.AddIdentity( o => { o.Password.RequireDigit = false; o.Password.RequireLowercase = false; o.Password.RequireUppercase = false; o.Password.RequireNonLetterOrDigit = false; o.Password.RequiredLength = 2; //o.User.AllowedUserNameCharacters here seems to be the only logical thing I can access }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); 我找到了o.User.AllowedUserNameCharacters ,但由于没有我在此发现的文档,我不知道我必须设置哪种格式? 在那里的任何人都在同一条船上工作了吗? 提前致谢。

为什么Scoped服务解析为同一请求的两个不同实例?

我有一个包含List的简单服务。 在Startup.cs中,我使用的是services.addScoped()方法。 我在两个不同的地方(控制器和中间件)注入服务实例,对于单个请求,我希望获得相同的实例。 但是,这似乎并没有发生。 即使我在Controller Action中向List添加Foo,中间件中的Foo列表也始终为空。 为什么是这样? 我已经尝试使用AddSingleton()将服务注册更改为单例,并且它按预期工作。 但是,这必须限制在当前请求范围内。 非常感谢任何帮助或想法! FooService.cs public class FooService { public List Foos = new List(); } Startup.cs … public void ConfigureServices(IServiceCollection services) { … services.AddScoped(); } [以下是我注入服务的两个地方,导致两个不同的实例] MyController.cs public class MyController : Controller { public MyController(FooService fooService) { this.fooService = fooService; } [HttpPost] public void TestAddFoo() { //add foo […]