August 16, 2016 20:46 by
Peter
In this today's tutorial, I will show you how to call rest API in ASP.NET. As the Internet industry progresses, creating a REST API becomes more concrete with emerging best practices.As RESTful web services don't follow a prescribed standard except for HTTP, it's important to build your RESTful API in accordance with industry best practices to ease development and increase client adoption. A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data.
RESTful APIs explicitly take advantage of HTTP methodologies defined by the RFC 2616 protocol. They simply use "PUT" to change the state of or update a resource, which can be an object, file or block; "GET" to retrieve a resource; POST" to create that resource; and "DELETE" to remove it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Data;
using System.Data.Common;
using Golive_Web.Controller;
namespace Golive.Controller
{
public class APITEXT
{
Database database = new Database("GoliveAPI");
public DataTable GetAPI()
{
DbCommand cmd = database.GetStoredPocCommand("SP_TExt");
DataTable dtApi = database.ExecuteDataTable(cmd);
return dtApi;
}
public string GetData(String mobileNo, String ID, String APILink)
{
try
{
string XML = "<AccDtls>" + "<Request>" + "<RequestUUID>Req_" + ID + "</RequestUUID>" + "<ServiceRequestId>AcctInq</ServiceRequestId>" + "<ServiceRequestVersion>10.2</ServiceRequestVersion>" + "<ChannelId>CTS</ChannelId>" + "<AccID>" + mobileNo + "</AccID> " + "</Request>" + "</AccDtls>";
const string url = "http://100.98.2.208:6070/rbl/CTSFin";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);
request.ContentType = "application/xml"; //set the content type to JSON
request.Method = "POST"; //make an HTTP POST
#
region test# endregion
using(var streamWriter = new StreamWriter(request.GetRequestStream()))
{
var resToWrite = XML;
streamWriter.Write(resToWrite);
streamWriter.Flush();
streamWriter.Close();
}
// Get the response.
WebResponse response = request.GetResponse();
var streamReader = new StreamReader(response.GetResponseStream());
string result = streamReader.ReadToEnd();
return result;
}
catch (Exception)
{
throw;
}
}
}
}
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.
August 3, 2016 23:41 by
Peter
I am gonna explaining how to read the data from CSV file using file upload control. A CSV is a comma separated values file, which allows data to be saved in a table structured format. CSVs look like a garden-variety spreadsheet but with a .csv extension (Traditionally they take the form of a text file containing information separated by commas, hence the name). CSV files can be used with any spreadsheet program, such as Microsoft Excel, Open Office Calc, or Google Spreadsheets. They differ from other spreadsheet file types in that you can only have a single sheet in a file, they can not save cell, column, or row styling, and can not save formulas.
Code
<asp:FileUpload ID="fpContacts" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" /></p>
Here we need to add a new folder to the solution, why because, sometimes it will takes the fake file path. it is better to save the file and read data.
In .cs page, write the following code.
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fpContacts.HasFile)
{
string spath = Server.MapPath("~/upload");
string csv_file_path = spath + "\\" + fpContacts.FileName;
fpContacts.SaveAs(csv_file_path);
DataTable csvData = GetDataTableFromCSVFile(csv_file_path);
}
}
public DataTable GetDataTableFromCSVFile(string csv_file_path)
{
DataTable csvData = new DataTable();
try
{
using (TextFieldParser csvReader = new TextFieldParser(csv_file_path))
{
csvReader.SetDelimiters(new string[] { "," });
csvReader.HasFieldsEnclosedInQuotes = true;
//read column names
string[] colFields = csvReader.ReadFields();
foreach (string column in colFields)
{
DataColumn datecolumn = new DataColumn(column);
datecolumn.AllowDBNull = true;
csvData.Columns.Add(datecolumn);
}
while (!csvReader.EndOfData)
{
string[] fieldData = csvReader.ReadFields();
ContactEntity CEntity = new ContactEntity();
B2B_BillsData BData = new B2B_BillsData();
CEntity.ContactName = fieldData[0];
CEntity.Email = fieldData[1];
CEntity.Mobile = fieldData[2];
CEntity.GroupId = ddlGroup.SelectedIndex;
CEntity.UserId = userid;
if (BData.InsertNewContact(CEntity) == true)
{
lblMsg.ForeColor = Color.Green;
lblMsg.Text = "Contact Saved successfully";
}
csvData.Rows.Add(fieldData);
}
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
}
return csvData;
}
European best, cheap and reliable ASP.NET hosting with instant activation. HostForLIFE.eu is #1 Recommended Windows and ASP.NET hosting in European Continent. With 99.99% Uptime Guaranteed of Relibility, Stability and Performace. HostForLIFE.eu security team is constantly monitoring the entire network for unusual behaviour. We deliver hosting solution including Shared hosting, Cloud hosting, Reseller hosting, Dedicated Servers, and IT as Service for companies of all size.