asp.net-core-mvc

How do you initialize application state at startup and access it from controllers in MVC 6?

心不动则不痛 提交于 2019-12-05 15:00:17
Let's say I have a class called MySiteConfiguration in which I have a bunch of, you guessed it, configuration data. This data will not change over the course of the application's runtime after it has been loaded. My goal would be to construct an instance of this class at startup and access it from my controller actions. I do not want to construct the class more than once, as this should not be needed. To do this in WebApi 2 for instance, I would: Instantiate the class in my application start method. Store the instance on the HttpConfiguration.Properties Create a ControllerBase class which

Redirect from exception filter

折月煮酒 提交于 2019-12-05 13:07:43
I'm using ASP.NET Core. One of my controllers calls into services which throw various exceptions. I want to handle them in an exception filter (not middleware). public class MyHandlerAttribute : ExceptionFilterAttribute { public override void OnException(ExceptionContext c) { if (c.Exception is FooException) { // redirect with arguments to here } else if (c.Exception is FooException) { // redirect with arguments to there } else { // redirect to main error action without arguments, as 500 } base.OnException(c); } } Unlike action filters, an exception filter doesn't give me access to the

InvalidOperationException rendering ViewComponent in Strongly-Typed View

半城伤御伤魂 提交于 2019-12-05 12:51:32
Recently updated dotnet core 1.0.1 to 1.1 and ViewComponent in MVC starts failing with the below exception: InvalidOperationException: One or more errors occurred. (The model item passed into the ViewDataDictionary is of type 'App.Models.HomeViewModel', but this ViewDataDictionary instance requires a model item of type 'App.Components.LoginViewComponent'.) The Index.cshtml renders LoginViewComponent : @model App.Models.HomeViewModel <html> @Component.InvokeAsync("LoginViewComponent") </html> Views/Home/Index.cshtml The ViewComponent LoginViewComponent/Default.cshtml is just a class that

Route Name for HttpGet attribute Name for base generic controller class in asp.net core 2

依然范特西╮ 提交于 2019-12-05 12:45:36
问题 I have a generic controller, which have several derived controller classes. but I cannot figure out how to handle the HttpGet's route name since it require constant. [HttpGet("{id}", Name ="should not hard coded here for derived class")] public virtual async Task<IActionResult> Get(int id) I need the route name because in my HttpPost function I want to return CreatedAtRoute() which require HttpGet's route name The route name cannot be hard coded because all the derived class need to have a

Checkbox not working with boolean viewmodel property

China☆狼群 提交于 2019-12-05 12:39:12
问题 I am using MVC6 and have a checkbox input field in my form, but when the form is submitted the value for the checkbox always gets passed to the ViewModel as false: Here is how the property is declared in my ViewModel: [Display(Name = "Include Sales Tax")] public bool IncludeSalesTax { get; set; } Here is how the form looks in my MVC6 razor form: <div class="form-group"> <div class="checkbox"> <label><input asp-for="IncludeSalesTax" type="checkbox" value="">@Html.DisplayNameFor(m => m

ASP.NET Core Identity 3 Cookie timeout

不想你离开。 提交于 2019-12-05 12:33:47
I have a weird issue happening with RC2. I have setup Identity 3 ExpireTimeSpan to 12 hours using the following configuration option options.Cookies.ApplicationCookie.ExpireTimeSpan = new TimeSpan(12,0,0); After logging in to the website and leaving it untouched for ~35-40mins, I get a 401 error (for my ajax post calls) and refreshing the website, I get back to the login page. Why do I have to reauthenticate when I have setup the ExpireTimeSpan to 12hours? Is there another setting or configuration that I need? Also, How can I get the time left before the expiry occurs? I would like to access

Publish to Azure fails with “ Unrecognized link extension 'contentLibExtension'” Error

亡梦爱人 提交于 2019-12-05 12:04:57
I am trying to publish ASP.NET MVC 6 App to Azure with Visual Studio 2015 CTP. And getting the following error Publishing with publish method [MSDeploy] Calling msdeploy with the command: ["C:\Program Files (x86)\IIS\Microsoft Web Deploy\msdeploy.exe" -source:IisApp='C:\Users\*****\AppData\Local\Temp\AspNetPublish\NgCalendar-28\wwwroot' -dest:IisApp='ngcalendar',ComputerName='https://ngcalendar.scm.azurewebsites.net/msdeploy.axd',UserName='$ngcalendar',Password='{PASSWORD-REMOVED-FROM-LOG}',IncludeAcls='False',AuthType='Basic' -verb:sync -enableLink:contentLibExtension -enableRule

Is there any good reason NOT to use a ViewComponent instead of a Partial View in core MVC?

 ̄綄美尐妖づ 提交于 2019-12-05 11:55:23
问题 I'm new to MVC and decided to start with .net-core, so I don't have much understanding of the differences in core vs. older versions. I did find the below question which offers some insight but hasn't helped me to decide whether I can basically ignore partial views. Why should we use MVC 6 Feature View Components over Partial View: What is the difference? My question is simply - if I can do something with a ViewComponent, is there any good reason not to? Many Thanks! Example provided below

WebTelemetryInitializerBase in ASP.NET Core / MVC6

烈酒焚心 提交于 2019-12-05 11:54:18
Is there an MVC6 compatible version of WebTelemetryInitializerBase that would work with ASP.NET Core (on the full .NET Framework)? See my question here where I asked how to get HttpContext in my temeletry initializers. Unfortunately I didn't specify that I was using MVC 6 and thus no System.Web.HttpContext . Yes, there is a version of this for aspnetcore. Check out the Microsoft Application Insights for ASP.NET Core applications repo. There is an implementation of getting the WebUser found in /src/Microsoft.ApplicationInsights.AspNetCore/TelemetryInitializers/WebUserTelemetryInitializer.cs

Custom ClaimsPrincipal in Asp.Net 5 (Mvc 6)

不羁岁月 提交于 2019-12-05 11:49:54
How can I override the default Context.User of type System.Security.Claims.ClaimsPrincipal in Asp.Net 5 (MVC-6)? I would like to use my customized User-type, so that it's accessible in Controllers ( HttpContext.User ), as well as in the Razor views ( @User. ) Any help is much appreciated. Thanks!:) The basic answer is don't - it's generally a bad idea to try to implement your own security code; there's lots of options out there that will save you a lot of time up front, and save you a lot of headaches on the back side, too. The other answer is that you can't - it's built in to the new