asp.net-core-mvc

How access ViewContext from helper/service, @injected via ViewImports

独自空忆成欢 提交于 2019-12-23 08:29:49
问题 I'm writing helper class and inject in in _ViewImports with @inject HtmlHelperInject.TestHelper TestHelper And register in Startup.ConfigureServices with services.AddTransient<TestHelper>(); How can I obtain ViewContext in this helper class? I tried injecting via controler - not working, via [ViewContext] attribute on property - not working. 回答1: As of right now (beta8) the way to do this is to implement... wait for it... ICanHasViewContext . This interface adds the following contract: void

ASP.Net Core MVC Repository Pattern Unexpectedly disposing

こ雲淡風輕ζ 提交于 2019-12-23 08:07:11
问题 When I try to add a comment, I get the following error: ObjectDisposedException: Cannot access a disposed object. When the code runs the second line: m_context.Comments.Add(comment); m_context.SaveChanges(); Why is the context being disposed? If move the the TryAddComment method into the controller, it doesn't call Dispose early. Here is what my Controller and Repository class look like (simplified). CommentsController.cs: public class CommentsController : Controller { private

'SignInScheme' option must be provided

≯℡__Kan透↙ 提交于 2019-12-23 07:56:14
问题 I'm creating an ASP.NET 5 MVC 6 app that will use Facebook/Google authentication only. I'm also trying to use the cookie middleware without the whole ASP.NET Identity -- following this article: https://docs.asp.net/en/latest/security/authentication/cookie.html So I started with an blank app with no authentication then added the Microsoft.AspNet.Authentication.Cookies and Microsoft.AspNet.Authentication.Facebook NuGet packages in order to have a very minimalistic approach where I don't include

using simple injector in mvc6 with cookie auth

蓝咒 提交于 2019-12-23 05:51:52
问题 I have a MVC6 project using simple injector and cookie middleware for authentication without ASP.NET identity (tutorials below) http://simpleinjector.readthedocs.org/en/latest/aspnetintegration.html http://docs.asp.net/en/latest/security/authentication/cookie.html I have a custom SignInManager / UserManager that wraps PrincipalContext to validate windows credentials (SideNote: I am not using the Azure AD with aspnet 5 because [in the future] I know there will be a mix of windows and non

Asp.Net Core Area routing to Api Controller not working

南楼画角 提交于 2019-12-23 04:58:22
问题 I have an API controller hosted in an area. However, the routing doesn't seem to be working as my ajax calls keep returning 404's when trying to hit the controller actions. Breakpoints in the controller constructor are never hit. [Area("WorldBuilder")] [Route("api/[controller]")] [ApiController] public class WorldApiController : ControllerBase { IWorldService _worldService; IUserRepository _userRepository; public WorldApiController(IWorldService worldService, IUserRepository userRepository) {

Locate Razor Pages in another assembly

泄露秘密 提交于 2019-12-23 03:44:10
问题 I want to locate My Project Razor Pages in another assembly. for doing this I write following code: public void ConfigureServices(IServiceCollection services) { var adminAssembly = Assembly.Load(new AssemblyName("App")); services.AddMvc().AddApplicationPart(adminAssembly).AddRazorOptions(options => { var previous = options.CompilationCallback; options.CompilationCallback = context => { previous?.Invoke(context); context.Compilation = context.Compilation.AddReferences( MetadataReference

Identity Server 4 (2.0) not reading Asp.Net Core Identity cookies

霸气de小男生 提交于 2019-12-23 03:31:40
问题 I am trying to use Asp .Net Identity Core with Identity Server 4. I can see in the logs (Ids) that the user is logged in properly. info: Xena.IdentityServer.Controllers.AccountController[0] User logged in. My login controller then sends the user over to my Manage controller. [Route("[controller]/[action]")] [Authorize] //[Authorize(AuthenticationSchemes = "Identity.Application")] public class ManageController : Controller { [HttpGet] public async Task<IActionResult> Index(ManageMessageId?

Angular2 component without view annotation

雨燕双飞 提交于 2019-12-23 03:10:07
问题 I would like to be able to use Angular2 to do client-side databinding on my server-rendered pages (ASP.Net MVC6). Is it possible to do this without adding a @view template? Instead of defining it inline or creating an external template, I would like to enclose a block of server-rendered HTML with the app element. This was how I did it in Angular1, as it allows me to choose whether to databind on the server-side or on the client-side. Thanks. 回答1: You can do something similar, that is probably

Routed localization in identity pages in ASP.NET MVC Core

北战南征 提交于 2019-12-23 02:01:34
问题 I am currently developing a web application in ASP.NET MVC Core where users should register. This is a localized web application that should be able to run for multiple languages. To be SEO friendly, I've chosen for routed localization, so my url's look like: https://localhost:5001/en/Catalogue or https://localhost:5001/fr/catalogue. To allow this, I added this piece of code in my ConfigureServices method in Startup.cs services.AddLocalization(options => options.ResourcesPath = "Resources");

How to display Partial view Model errors when partial view is in the view?

大城市里の小女人 提交于 2019-12-23 01:36:15
问题 I am working on the Asp.net Core 2.0 project and have a partial view that is shown in the index view. This partial view have a Model with some validation errors. You can continue to see my Model,Partial view,controller and ... LoginViewModel public class LoginViewModel { [Display(Name = "UserName")] [Required(AllowEmptyStrings = false, ErrorMessage = "Please Enter UserName")] public string UserName { get; set; } [Display(Name = "Password")] [Required(AllowEmptyStrings = false, ErrorMessage =