asp.net-core-mvc

Unable to create migrations after upgrading to ASP.NET Core 2.0

心不动则不痛 提交于 2019-12-17 06:29:00
问题 After upgrading to ASP.NET Core 2.0, I can't seem to create migrations anymore. I'm getting "An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: One or more errors occurred. (Cannot open database "..." requested by the login. The login failed. Login failed for user '...'" and "Unable to create an object of type 'MyContext'. Add an implementation of 'IDesignTimeDbContextFactory' to the project, or see https://go

Resolving instances with ASP.NET Core DI

爱⌒轻易说出口 提交于 2019-12-17 02:40:19
问题 How do I manually resolve a type using the ASP.NET Core MVC built-in dependency injection framework? Setting up the container is easy enough: public void ConfigureServices(IServiceCollection services) { // ... services.AddTransient<ISomeService, SomeConcreteService>(); } But how can I resolve ISomeService without performing injection? For example, I want to do this: ISomeService service = services.Resolve<ISomeService>(); There are no such methods in IServiceCollection . 回答1: The

Custom cookie authentication not working after migration from ASP.NET Core 1.1 MVC to 2.0

匆匆过客 提交于 2019-12-14 04:16:46
问题 I have migrated an ASP.NET Core 1.1 MVC project to ASP.NET Core 2.0 and now I note that requests to unauthorized sections of the application no longer result with a "401 Unauthorized" response but rather with a code exception leading to a response "500 internal server error". An example excerpt from the log file (John Smith is not authorized to acces the controller action he tried to access): 2018-01-02 19:58:23 [DBG] Request successfully matched the route with name '"modules"' and template '

HttpResponse.Filter in MVC 6 / vNext?

断了今生、忘了曾经 提交于 2019-12-14 03:54:28
问题 I'm working on a website framework that uses "Widgets" to render content chunks inside of a portal/portlet type website. Basically a very abbreviated version of how Wordpress lets you customize a website. Having searched and failed at finding any existing frameworks, it seems like the most reasonable way to implement this is to use the "Partial Views" feature of MVC. I'd like to be able to fully encapsulate each "widget" without having to repeat script or style tags in the host View (Views

Set culture and ui-culture in appsettings.json (asp.net core localization)?

ⅰ亾dé卋堺 提交于 2019-12-14 03:46:30
问题 I have asp.net core application which I want to localize and translate (v 1.1.0). I want the source code to support both english and norwegian deployments, and I found this could be a startup.cs configuration RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions { SupportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("nb-NO") }, SupportedUICultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("nb-NO") },

How to access claims of other than current user in Microsoft Identity 3

余生长醉 提交于 2019-12-14 03:22:47
问题 I'm using Microsoft Identity 3. I can access the claims of the current user. But I can't figure out how to do the same for another user. For the current user, inside the controller, I can access the claims collection via: IEnumerable<Claim> claims = User.Claims; I can see all the user claims and I can add a claim as follows: var user = await GetCurrentUserAsync(); await _userManager.AddClaimAsync(user, new Claim("role", "manager")); But if I do this: IdentityUser user = await _userManager

Controller Action not triggered - Simple Project - ASP.NET MVC CORE 2.0

无人久伴 提交于 2019-12-14 03:15:39
问题 I have a very simple web application, where I try to let people register a user. Therefore, I try to save user-registration data to Entity Framework, but with no luck. For some reason, IActionResult RegisterUser isn't triggered when submitting a form (I tried setting breakpoints, nothing happened). Can anyone detect why? Startup.cs public class Startup { public void ConfigureServices(IServiceCollection services) { var connection = @"Server=(localdb)\mssqllocaldb;Database=APIExercise2;Trusted

using moq with asp.net core while unit testing

两盒软妹~` 提交于 2019-12-14 02:23:45
问题 I am trying to write a very simple unit test in asp.net core using moq and xunit. But I am getting a error when I run the unit test. StackTrace: at Moq.Mock 1..ctor(MockBehavior behavior, Object[] args) at Moq.Mock 1..ctor(MockBehavior behavior) at Moq.Mock`1..ctor() Could not load file or assembly 'System.Core, version=4.0.0'. Below is my code and project.json file. { "version": "0.1.0-*", "testRunner": "xunit", "dependencies": { "Moq": "4.5.22", "xunit": "2.2.0-beta2-build3300", "dotnet

DIRECT replacement of UserHostAddress in ASP.NET Core?

自闭症网瘾萝莉.ら 提交于 2019-12-14 01:51:24
问题 We discovered that Request.UserHostAddress is not available in ASP.NET Core (1.0 RC1), as well as Request.ServerVariables["REMOTE_ADDR"] , which returns essentially the same. Now we found HttpContext.Connection.RemoteIpAddress or HttpContext.Features.Get<IHttpConnectionFeature>()?.RemoteIpAddress to be the replacement, but is it really a 1:1 replacement for the functionality? What I mean: Request.ServerVariables["REMOTE_ADDR"] only returns the direct IP address of a caller, not a forwarded

Automatically switching views for AMP in ASP.NET MVC

ε祈祈猫儿з 提交于 2019-12-14 00:36:04
问题 I want to create and AMP version of my website in ASP.NET MVC using .NET Core 2.0. Previously I had done some work with DisplayModeProvider instances in tha past on .Net framework, but that does not seem to be an option in .NET Core. What I want to be able to do is alter the view names to be index.amp.cshtml rather than index.cshtml when my URL starts iwth /amp . What's the best way to achieve this in .NET Core? 回答1: You can do something like this using IViewLocationExpander . As it happens,