Showing posts with label .Net Core. Show all posts
Showing posts with label .Net Core. Show all posts

Wednesday, March 6, 2019

Traditional file helper won't work in .Net core

Prior to .Net Core, we use to handle file by passing various sort of parameters, like -in memory bytes, FileStream or file path and that use to work perfectly. 

But when it comes to .Net Core, passing a file path will not work exactly as ASP.Net MVC. In earlier versions, the path we supplied was considered as a physical path whereas in Core, same API is used to denote the virtual path. In other words, whatever path is provided will be appended with site URL. 

Now how to give physical path in .Net Core? 
No worries! Here comes the PhysicalFile helper for our rescue. To know more about it, here you go.

Keep learning!

Monday, January 1, 2018

Invoking web application from console application (.Net Core) via command prompt

In this article, I’ll be giving a walkthrough on how to create a console application and changing that into a web application. Or in other words, invoking a web application from a .Net core console application. And that too completely from command prompt. If you are a command prompt lover, you may love it. So, let’s gear up and proceed step-by-step.

Verify .Net Core
If you are creating a .Net Core application for the very first time, then it is good to verify whether it is installed on the system or not and this can be done by typing a simple command dotnet --version as shown below: 







Create Console application
Now we are sure that required setup is present on our machine, we can proceed and create our first Console application using command dotnet new console as shown below: 









On successful execution of above command, you will see that Program file and project file is created in your local directory and same can be verified by opening Program.cs in notepad using below command:





Building and Running Console application
Next step would be to see the output from the console application and that can be done by run command as shown below:






In above screen shot you can see that output “Hello World!” is shown on the screen which means both compilation and execution will be done using single command.
Console app into a Web app
For any web application, first we have to add dependency packages. So, let’s go ahead and add reference of AspNetCore library from Nuget and that too via command line as shown below:





In above screenshot, you can see that dependent package is added to .csproj file. Run your application and you would still be able to see the console application output as we didn’t change our app. If you are facing any reference related errors then run the restore command as dotnet restore, and things will be alright.
Add Startup file for Web application
As a practice, usually any web application starts with a Startup.cs file, we will also go ahead and add a Startup.cs file in our project with basic skeleton and namespaces added as shown below:





Next is to fill in the missing parts of Startup class file to make our code functional. Let’s quickly add the code to the Startup class:
  1. using System;  
  2. using Microsoft.AspNetCore.Builder;  
  3. using Microsoft.AspNetCore.Hosting;  
  4. using Microsoft.AspNetCore.Http;  
  5.   
  6. namespace SampleCore  
  7. {  
  8.    public class Startup  
  9.    {  
  10.     public void Configure(IApplicationBuilder builder)  
  11.         {  
  12.         builder.Run(appContext =>   
  13.          {  
  14.         return appContext.Response.WriteAsync("Hey, I'm from Web!");  
  15.          });  
  16.         }  
  17.    }  
  18. }  

Hooking up the web application in console
At this point, if you will run the application, you will still get the output which is mentioned in Program.cs because we have not told the Main() about our Startup class.

So, let’s quickly go ahead and plug our web application into console application. Below is the code to do so:
  1. static void Main(string[] args)  
  2.         {  
  3.             var hostBuilder = new WebHostBuilder()  
  4.             .UseKestrel() //tiny web server. It can be replaced with any web server  
  5.             .UseStartup<Startup>()  
  6.             .Build();  
  7.       
  8.         hostBuilder.Run();  
  9.         }  
Now we are all set to run our application.
Launching an application 
Go back to command prompt and fire dotnet run and you will see that your application is now an web application with web server up and running.







Hope you like this. Enjoy reading.

Sunday, December 24, 2017

Ways to add dependency packages in .NET core

This would be a very short article on how to add dependencies in .Net Core.

Well, there are many ways to achieve this. One is via Visual Studio and another way is through command prompt. Let’s quickly have a look. 

1) From Visual Studio: 
This is the straight forward way for the ones who want to use user interface to add dependencies. Right click on your project/library and get it from Nuget gallery. 

2) From Command Prompt: 
If you are a command prompt fan, then there itself, you have 2 choices.
A) Open command prompt. Navigate to your project directory and simply fire:

C:\<Your project directory> dotnet add package Microsoft.AspNetCore 

B) Alternative you can go and add the reference directly in .csproj file as shown below:











and restore it from command prompt using very simple command:

C:\<Your project directory> dotnet restore

Happy learning!!!