asp.net-core-mvc

How to Per-Request caching in ASP.net core

陌路散爱 提交于 2019-12-03 23:43:25
My old code looks like this: public static class DbHelper { // One conection per request public static Database CurrentDb() { if (HttpContext.Current.Items["CurrentDb"] == null) { var retval = new DatabaseWithMVCMiniProfiler("MainConnectionString"); HttpContext.Current.Items["CurrentDb"] = retval; return retval; } return (Database)HttpContext.Current.Items["CurrentDb"]; } } Since we don't have HttpContext anymore easily accesible in core, how can I achieve the same thing? I need to access CurrentDb() easily from everywhere Would like to use something like MemoryCache, but with Request lifetime

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

人走茶凉 提交于 2019-12-03 23:34:57
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 different route name. here is the base controller public abstract class BaseController<TEntity, TContext>

Checkbox not working with boolean viewmodel property

强颜欢笑 提交于 2019-12-03 23:34:35
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.IncludeSalesTax)</label> </div> </div> I figured the above would be the best way to follow Twitter Bootstrap

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

自闭症网瘾萝莉.ら 提交于 2019-12-03 23:32:47
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 for context. Main view calls: ViewComponent: <div class="modal-body" ID="modalPersonInner"> @await

Opening a websocket channel inside MVC controller

梦想的初衷 提交于 2019-12-03 23:04:45
Has anyone has any good experience with opening a websocket connection inside MVC controller? Technology stack: ASPNET Core 1.0 (RC1) MVC, dnx46, System.Net.WebSockets Why MVC instead of middleware: for overall consistency, routing, already injected repositories, an option to call private methods in the same controller. [HttpGet("v1/resources/{id}")] public async Task<IActionResult> GetAsync(string id) { var resource = await this.repository.GetAsync(id); if (resource == null) { return new HttpStatusCodeResult(404); } if (this.HttpContext.WebSockets.IsWebSocketRequest) { var webSocket = await

Configure cors to allow all subdomains using ASP.NET Core (Asp.net 5, MVC6, VNext)

青春壹個敷衍的年華 提交于 2019-12-03 22:21:04
I have cors setup correctly in an ASP.NET Core web app. Im using the following package... "Microsoft.AspNet.Cors": "6.0.0-rc1-final" and here is the startup.cs snippet... public virtual IServiceProvider ConfigureServices(IServiceCollection services) { services.AddCors ( options => { options.AddPolicy ( CORSDefaults.PolicyName, builder => { //From config... var allowedDomains = new []{"http://aaa.somewhere.com","https://aaa.somewhere.com","http://bbb.somewhere.com","https://bbb.somewhere.com"}; //Load it builder .WithOrigins(allowedDomains) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(

reference is not added in ASP.Net MVC 6 for \"Microsoft.BingAds.SDK”

依然范特西╮ 提交于 2019-12-03 21:26:51
I am doing this project in mvc6 using asp.net framework 6.how I can add this reference in my project please help me. You have to talk to BingAds and ask them for a compatible version. This is the same with every library provider, they all must support the new platform. You cannot add a library which targets .NET 4.x, they must rewrite and publish a new version/ 来源: https://stackoverflow.com/questions/36194075/reference-is-not-added-in-asp-net-mvc-6-for-microsoft-bingads-sdk

MVC Core IActionResult meaning

帅比萌擦擦* 提交于 2019-12-03 20:09:29
What is an IActionResult ? I tried looking at MSDN and other sites, but need general, common easy to understand answer. MSDN IActionResult Example: public IActionResult About() { ViewData["Message"] = "About Page"; return View(); } In general terms IActionResult type is a base abstraction of an action result. It is used as the base of other derived action results that represent specific response types, of which there are many. Reference Asp.Net Core Action Results Explained IActionResult and ActionResult IActionResult and ActionResult work as a container for other action results, in that

How to configure multiple ASPNETCORE_ENVIRONMENT on same machine?

给你一囗甜甜゛ 提交于 2019-12-03 19:05:05
问题 I have ASP.NET core web application. I have configured the web application on our web server and set the ASPNETCORE_ENVIRONMENT variable to Development . I set this variable at machine level like shown in the picture below. Now on the same machine i want to configured one more instance of same web application as Staging environment. What are my options here to set ASPNETCORE_ENVIRONMENT at application level instead of machine level? so i can host multiple instances of the same application on

How do I dynamically choose a controller in MVC Core

家住魔仙堡 提交于 2019-12-03 18:59:14
I have a situation where a site may need a link to redirect to certain controllers based on database results. For example: site.com/abcd needs to return the result from a Item Controller, which would normally be called as /item/view/123 The key here is that I can't hard code the abcd into the routing. And some links may go to an Item Controller, others may go to an Orders controller. I've tried a catchall route to a controller than then loads up the desired controller, but the environment is not set so it does not work properly (it can't find the views). NightOwl888 You can get whatever