Tag: asp.net core 1.0

在单独的项目Asp.net Core MVC中进行本地化

我刚刚升级到Rc2,而以前的工作不再适用。 我在一个单独的项目中有几个resx文件,我使用自定义类来访问数据。 现在运行时出现以下错误: MissingManifestResourceException:找不到适合指定文化或中性文化的任何资源。 确保在编译时将“GarageWeb.Core.CoreResources.resources”正确嵌入或链接到程序集“GarageWeb.Core”中,或者所有所需的附属程序集都是可加载和完全签名的。 编辑:我简化了这一点,并创建了一个控制台应用程序,除了重现错误所需的一切,所以除去了所有内容: https : //github.com/GarageWeb/ResourceTest 这是访问资源的类: public class ResourceService : IResourceService { private readonly ILoggingService _loggingService; private readonly ICoreGlobalResourceService _coreGlobalResources; private readonly ISiteGlobalResourceService _siteGlobalResources; public ResourceService(ILoggingService loggingService, ICoreGlobalResourceService coreGlobalResourceService, ISiteGlobalResourceService siteGlobalResources) { _loggingService = loggingService; _coreGlobalResources = coreGlobalResourceService; _siteGlobalResources = siteGlobalResources; } public string GetGlobalText(string resourceKey, bool includeBrackets = true) { […]

如何使用ASP.NET Core进行流式处理

如何正确地在ASP.NET Core中传输响应? 有这样的控制器( 更新代码 ): [HttpGet(“test”)] public async Task GetTest() { HttpContext.Response.ContentType = “text/plain”; using (var writer = new StreamWriter(HttpContext.Response.Body)) await writer.WriteLineAsync(“Hello World”); } Firefox / Edge浏览器显示 你好,世界 ,Chrome / Postman报告错误: localhost页面无效 localhost意外关闭了连接。 ERR_INCOMPLETE_CHUNKED_ENCODING PS我即将流式传输大量内容,因此我无法提前指定Content-Length标头。

使用XUnit和ASP.NET Core 1.0进行dependency injection

我试图找出如何使用XUnit的dependency injection。 我的目标是能够将我的ProductRepository注入我的测试类。 这是我正在尝试的代码: public class DatabaseFixture : IDisposable { private readonly TestServer _server; public DatabaseFixture() { _server = new TestServer(TestServer.CreateBuilder().UseStartup()); } public void Dispose() { // … clean up test data from the database … } } public class MyTests : IClassFixture { DatabaseFixture _fixture; public ICustomerRepository _repository { get; set; } public MyTests(DatabaseFixture […]

如何将IFormFile保存到磁盘?

我正在尝试使用这段代码将文件保存在磁盘上。 IHostingEnvironment _hostingEnvironment; public ProfileController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; } [HttpPost] public async Task Upload(IList files) { foreach (var file in files) { var fileName = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim(‘”‘); var filePath = _hostingEnvironment.WebRootPath + “\\wwwroot\\” + fileName; await file.SaveAsAsync(filePath); } return View(); } 我能够用IHostingEnvironment替换IApplicationEnvironment,用WebRootPath替换ApplicationBasePath 。 似乎IFormFile不再具有SaveAsAsync() 。 如何将文件保存到磁盘呢?

在dotnet核心中运行NUnit测试

我正在尝试使用dotnet core为我的c#项目运行unit testing。 我正在使用docker容器进行运行时。 Dockerfile FROM microsoft/dotnet:0.0.1-alpha RUN mkdir /src WORKDIR /src ADD . /src RUN dotnet restore “NUnit”和“NUnit.Runners”已添加到project.json中 “version”: “1.0.0-*”, “compilationOptions”: { “emitEntryPoint”: true }, “dependencies”: { “NETStandard.Library”: “1.0.0-rc2-23811”, “NUnit”: “3.2.0”, “NUnit.Runners”: “3.2.0” }, “frameworks”: { “dnxcore50”: { } } 使用以下输出成功运行dotnet restore … log : Installing NUnit.ConsoleRunner 3.2.0. log : Installing NUnit.Extension.NUnitV2ResultWriter 3.2.0. log […]

使用Identity 2.0数据库来validationASP.NET Core 1.0应用程序

我正在尝试创建一个新的ASP.NET Core 1.0 Web应用程序,我希望它使用我已经设置的身份validation表。 这些表最初是由使用Microsoft.ASPNet.Identity.EntityFramework 2.2.0的ASP.NET 4.6 Web应用程序创建的。 看起来Microsoft.AspNetCore.Identity.EntityFrameworkCore中的内容发生了变化,因为新的Core 1.0应用程序在尝试登录时抛出此错误: 处理请求时数据库操作失败。 SqlException:无效的列名称’NormalizedUserName’。 列名称’ConcurrencyStamp’无效。 列名称“LockoutEnd”无效。 列名称“NormalizedEmail”无效。 列名称“NormalizedUserName”无效。 project.json是开箱即用的,看起来像这样: “dependencies”: { “Microsoft.NETCore.App”: { “version”: “1.0.0”, “type”: “platform” }, “Microsoft.AspNetCore.Authentication.Cookies”: “1.0.0”, “Microsoft.AspNetCore.Diagnostics”: “1.0.0”, “Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore”: “1.0.0”, “Microsoft.AspNetCore.Identity.EntityFrameworkCore”: “1.0.0”, “Microsoft.AspNetCore.Mvc”: “1.0.0”, “Microsoft.AspNetCore.Razor.Tools”: { “version”: “1.0.0-preview2-final”, “type”: “build” }, “Microsoft.AspNetCore.Server.IISIntegration”: “1.0.0”, “Microsoft.AspNetCore.Server.Kestrel”: “1.0.0”, “Microsoft.AspNetCore.StaticFiles”: “1.0.0”, “Microsoft.EntityFrameworkCore.SqlServer”: “1.0.0”, “Microsoft.EntityFrameworkCore.SqlServer.Design”: { “version”: “1.0.0”, […]

如何在#if语句中设置.NET Core以进行编译

我创建了multy框架项目。 我使用这样的东西: #if NET40 Console.WriteLine(“hello from net 4”); #endif 但我无法找到.NET Core的通配符。 我试过了 : #if NETCOREAPP1.0 Console.WriteLine(“hello from net Core”); #endif 但这不是有效的声明。 谢谢。

使用ASP .NET Core Identity和EntityFrameworkCore注册新用户时出现InvalidOperationException

我正在关注使用Identity的文档 ,我正在尝试注册一个新用户(执行注册操作),但它失败并出现以下错误: InvalidOperationException:无法为“ApplicationUser”创建DbSet,因为此类型未包含在上下文的模型中。 启动: services.AddIdentity(options => { //password options options.Password.RequireDigit = false; // … }) 我使用的是标准的ApplicationUser: public class ApplicationUser : IdentityUser { } 在AccountController中注册操作: public async Task Register(RegisterViewModel viewModel) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = viewModel.UserName, Email = viewModel.Email }; var result = await _userManager.CreateAsync(user, viewModel.Password); //<– Exception happens […]

引用.NET Core XUnit项目中的标准dll

我使用的是最新版本的XUnit,我按照这些步骤启动了类库(.NET Core)项目。 我整个解决方案中的所有其他库仅使用4.6.1,因此我将pr​​oject.json中的框架部分更改为以下内容: { “frameworks”: { “net461”: { “dependencies”: { “Microsoft.NETCore.Platforms”: “1.0.1-rc2-24027” } } } } 在我的解决方案中,一切正常。 我能够运行测试,所有对其他库的引用工作正常,即使它们只有461。 基本上,在我的解决方案中,我有几个其他类库(.NET Core),我的XUnit库依赖它们,所以我只能通过visual studio引用它们,并且引用被添加到XUnit项目的project.json文件中。 出现了我需要执行以下操作的情况: 仅将XUnit项目复制到另一台开发人员计算机。 允许他编辑源代码,以便能够创建测试,但不允许他访问所有其他库/代码。 我想我可以简单地将所有其他dll复制到一个随机文件夹中,并能够从独立的XUnit项目中引用它们。 但是,情况并非如此,因为在尝试引用时,我收到一条错误消息,指出.NET Core项目无法引用标准dll。 有人能告诉我如何做到这一点吗?

在mvc中使用IViewLocationExpander

我想从自定义位置渲染视图,所以为此我在类中实现了IViewLocationExpander接口。 我在startup file注册了相同的类,如下所示。 Startup.cs文件 public void ConfigureServices(IServiceCollection services) { ….. //Render view from custom location. services.Configure(options => { options.ViewLocationExpanders.Add(new CustomViewLocationExpander()); }); …. } CustomViewLocationExpander类 public class CustomViewLocationExpander : IViewLocationExpander { public IEnumerable ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable viewLocations) { var session = context.ActionContext.HttpContext.RequestServices.GetRequiredService(); string folderName = session.GetSession(“ApplicationType”); viewLocations = viewLocations.Select(f => f.Replace(“/Views/”, “/” + folderName + “/”)); […]