asp.net-core-mvc

Should I use AddMvc or AddMvcCore for ASP.NET Core MVC development?

假装没事ソ 提交于 2019-12-03 06:11:32
问题 I am learning ASP.NET Core MVC from a book, the code snippet in question is as follows: // CHAPTER 4 - ESSENTIAL C# FEATURES namespace LanguageFeatures { public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } // etc. Because the book is about ASP.NET Core MVC rather than ASP.NET MVC, I think I have to use AddMvcCore() rather than AddMvc() as follows: public void ConfigureServices(IServiceCollection services) { services.AddMvcCore(); // as

Does a Stream get Disposed when returning a File from an Action? [duplicate]

廉价感情. 提交于 2019-12-03 06:09:31
This question already has answers here : Does FileStreamResult close Stream? (1 answer) How do I dispose my filestream when implementing a file download in ASP.NET? (2 answers) I'm writing a string to a MemoryStream I need to return the stream to the Controller Action so I can send it off as a file for download. Normally, I wrap the Stream in a using statement, but, in this case, I need to return it. Does it still get Disposed after I return it? Or do I need to dispose it myself somehow? //inside CsvOutputFormatter public Stream GetStream(object genericObject) { var stream = new MemoryStream()

Logging within Program.cs

柔情痞子 提交于 2019-12-03 06:03:06
Is it possible to get an ILogger inside Program.cs Main method? I want to pass it on to a service that's created inside that method. I've only found this on SO How do I write logs from within Startup.cs , but that's logging within Startup.cs. Accidentally stumbled upon the answer after googling a bit more. using System; using Microsoft.Extensions.Logging; namespace ConsoleApplication { public class Program { public static void Main(string[] args) { var logFactory = new LoggerFactory() .AddConsole(LogLevel.Debug) .AddDebug(); var logger = logFactory.CreateLogger<Type>(); logger.LogInformation(

VersionedRoute Attribute Implementation for MVC6

做~自己de王妃 提交于 2019-12-03 05:53:08
问题 I am trying to enable versioning on a REST API, where the version is specified in the header, as "api-version":2 . According to this tutorial I just need to create VersionConstraint : IHttpRouteConstraint and VersionedRoute: RouteFactoryAttribute The usage would be to apply the [VersionedRoute("api/controllerName", 2)] Attribute to Controllers, which are designed for specific versions (e.g. version 2 in this case). This is all good and well, but unfortunately, it's all in MVC5 and I'm using

Run a background task from a controller action in asp.net core 2

拜拜、爱过 提交于 2019-12-03 05:44:35
问题 I am developing a web application with a REST Api using C# with asp.net core 2.0 What I want to achieve is when the client send a request to an endpoint I will run a background task separated from the client request context which will be ended if the task started successfully. I know there is HostedService but the problem is that the HostedService starts when the server starts, and as far as I know there is no way to start the HostedService manually from a controller. Here is a simple code

Cookies in ASP.Net MVC 5

删除回忆录丶 提交于 2019-12-03 04:58:38
I am developing an application in which users are SignUp or SignIn by External Identity Providers like AAD, Google, WS-Federated Authentication etc. Now I want to create cookies on a user machine to logged in until user SignOut. Give me some thought and guide me how I can overcome it. thanks in advance. Nikhil.Patel Use Request.Cookies and Response.Cookies to handle your situation. once user coming back from third party authorization create cookie and store it in browser and once user Logout clear the cookie. string cookievalue ; if ( Request.Cookies["cookie"] != null ) { cookievalue = Request

MVC6 alternative to @Html.DisplayFor

回眸只為那壹抹淺笑 提交于 2019-12-03 04:53:59
MVC6 introduces Tag Helpers which is a better way compared to using @Html.EditorFor, etc. However I have not found any Tag Helper that would be an alternative to @Html.DisplayFor. Of course I can use a variable directly on a Razor page, such as @Model.BookingCode. But this does not allow to control formatting. With MVC6, what's conceptually correct way for displaying a value of a model property? You can create your own tag helper namespace MyDemo.TagHelpers { [HtmlTargetElement("p", Attributes = ForAttributeName)] public class DisplayForTagHelper : TagHelper { private const string

App_Data directory in ASP.NET5 MVC6

冷暖自知 提交于 2019-12-03 04:46:32
I've been trying ASP.NET5 MVC6 app. In the previous version, there was a directory App_Data . I used this folder to store error logs. But it is not found in latest version. Any help? Robert Stevens This works for ASP.NET MVC with Core 2 public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // Use this code if you want the App_Data folder to be in wwwroot //string baseDir = env.WebRootPath; // Use this if you want App_Data off your project root folder string baseDir = env.ContentRootPath; AppDomain.CurrentDomain.SetData("DataDirectory", System.IO.Path.Combine(baseDir, "App

Unit testing routing in ASP.NET Core 1.0 (ex MVC 6)

大憨熊 提交于 2019-12-03 04:30:41
As the architecture of ASP.NET Core 1.0 (ex MVC 6 / ASP.NET 5.0) changed significantly, how would one go about unit testing the routing? As an example, I like the libraries such as this one (as for <= MVC 5): https://github.com/AnthonySteele/MvcRouteTester Something down the lines of fluent extension methods: routes.ShouldMap("/").To<HomeController>(x => x.Index()); Alright... I did ping the ASP.NET team and asked them how they proceeded to do their tests. Short answer You can't unit test without mocking the world. You have to do integration/functional tests. Slightly longer answer Routing can

Access from class library to appsetting.json in Asp.net-core

做~自己de王妃 提交于 2019-12-03 04:21:34
I am trying to access appsetting.json file from a class library. So far the solution that I found is to create a configuration class implementing interface IConfiguration from Microsoft.Extensions.Configuration and add the json file to class and read from the same. var configuration = new Configuration(); configuration.AddJsonFile("appsetting.json"); var connectionString= configuration.Get("connectionString"); This seems to be bad option as we have to add the json file each time we have to access the appsetting configuration. Dont we have any alternative like ConfigurationManager in ASP.NET .