Posts

Showing posts with the label WebApi

How to write api method to Read file and save it in ftp server using webapi c#

[Route("getdata")]         [HttpPost]         public string ReadFromAzureAS(string path)         {             //FTP Server URL.             string ftp = "ftp://localhost:21/";             //FTP Folder name. Leave blank if you want to upload to root folder.             string ftpFolder = "Uploads/";             string status = "File Transfer Failed";             FileStream stream = File.OpenRead(path);             byte[] fileBytes = new byte[stream.Length];             stream.Read(fileBytes, 0, fileBytes.Length);             stream.Close();             //Begins the process of writing the byte array back to a file      ...

Read any file using filepath and save it in a folder in c#

// Read file to byte array FileStream stream = File.OpenRead( @" c:\path\to\your\file\here.txt" ); byte [] fileBytes= new byte [stream.Length]; stream.Read(fileBytes, 0 , fileBytes.Length); stream.Close(); // Begins the process of writing the byte array back to a file using (Stream file = File.OpenWrite( @" c:\path\to\your\file\here.txt" )) { file.Write(fileBytes, 0 , fileBytes.Length); }

Implement basic authentication in web api c#

First, create an empty web API project . Then follow the below steps: Here I want to validate the user if the user exists then display the employee details based on username. 1. Create a User model class and Employee model. User Model Class Employee Model Class public class User     {         public int ID { get; set; }         public string UserName { get; set; }         public string Password { get; set; }     }     public class Employee     {         public int ID { get; set; }         public string Name { get; set; }         public string Gender { get; set; }         public string Dept { get; set; }    ...

How to send email from webapi using c#

Image
To Configure or send email from a web application or a web services, we can use the below code.  Here, we are configuring the mail settings in web config file, the configuration includes host,username,password and sender mail id.   Step 1: Add the below code in web-config Step 2:Paste the below code in web API controller         [HttpGet]         [Route("send-email")]         public async Task SendEmail()         {             var message = new MailMessage();             message.To.Add(new MailAddress("Abhinay" + " <" + "receivermailid@gmail.com" + ">"));             message.From = new MailAddress("AbhinayKumar ");             message.Bcc.Add(new MailAddress("AbhinayKumar "));             message.Subject = "test message"; ...

How to return only Json response from webapi method using c#?

Paste the below code in WebApiConfig.cs file, config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json")); config.Formatters.Remove(config.Formatters.XmlFormatter);

Implement Azure Ad Authentication In webapi c#

Install-Package Microsoft.AspNet.WebApi Install-Package Microsoft.AspNet.WebApi.Owin Install-Package Microsoft.Owin.Host.SystemWeb Install-Package Microsoft.Owin.Security.ActiveDirectory Install-Package Microsoft.Net.Http Install-Package Microsoft.IdentityModel.Clients.ActiveDirectory

How to retrieve json parameters from postman in webapi?

If we send parameters in text format we can make use of the following code and convert it into a class object . In your method copy the following code,                   1.var json = await Request.Content.ReadAsStringAsync();                       2. clsFeilds objJson = JsonConvert.DeserializeObject<clsFeilds>(json); In the above code stmt 1 retrieves the JSON parameters in JSON string format. In stmt 2 when we pass the JSON string it deserializes it to a class object. Note: To use the above code we need to make our method as '' ASYNC ". ex:         [HttpGet]         public async Task<string> multiple()         {             //        { "U...