Diễn đàn    Cổng thông tin , Chính phủ điện tử, VSP (VerySmart Portal), MVC Model View Controller, json, webapi    web api json

Thành viênTrả lời
aspnet

Lập trình không biên giới
608  bài
10-6-2019 16:13:23
public IHttpActionResult Get()
{
return Json("list");
}
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
10-6-2019 16:13:40
public HttpResponseMessage Post(WebA.KhachhangInfo inf)
{
try
{
WebA.KhachhangController db = new WebA.KhachhangController();
if (inf.ID == "") inf.ID = Guid.NewGuid().ToString("N");
db.Insert(inf);
}
catch (Exception ex)
{
return Request.CreateResponse(HttpStatusCode.OK, ex.Message + "==" + ex.StackTrace);
}

return Request.CreateResponse(HttpStatusCode.OK, "post");
}
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
10-6-2019 16:14:9
[code]

public HttpResponseMessage Put(WebA.KhachhangInfo inf)
{
WebA.KhachhangController db = new WebA.KhachhangController();
db.Update(inf);

return Request.CreateResponse(HttpStatusCode.OK, "put");
}

public HttpResponseMessage Delete(string id)
{
WebA.KhachhangController db = new WebA.KhachhangController();
db.Delete(id);

return Request.CreateResponse(HttpStatusCode.OK, "delete");
}
[/code]
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
10-6-2019 16:20:45
Upload photo to server

public HttpResponseMessage Post()
{
string ret_msg = "";
if (HttpContext.Current.Request.Files.Count > 0)
{
// Get the uploaded image from the Files collection
HttpPostedFile httpPostedFile = HttpContext.Current.Request.Files[0];

if (httpPostedFile != null)
{
// Validate the uploaded image(optional)
string ext = Path.GetExtension(httpPostedFile.FileName);
// Get the complete file path
string fileSavePath = "~/files/images/" + Guid.NewGuid().ToString("N") + ext;
ret_msg = fileSavePath;

// Save the uploaded file to "UploadedFiles" folder
httpPostedFile.SaveAs(HttpContext.Current.Server.MapPath(fileSavePath));

}
}
else
ret_msg = "no file";

return Request.CreateResponse(HttpStatusCode.OK, ret_msg);
}
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
10-6-2019 16:33:16
public string UploadFilesToRemoteUrl(string url, string[] files, NameValueCollection formFields = null)
{
string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "multipart/form-data; boundary=" +
boundary;
request.Method = "POST";
request.KeepAlive = true;

Stream memStream = new System.IO.MemoryStream();

var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
boundary + "\r\n");
var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" +
boundary + "--");


string formdataTemplate = "\r\n--" + boundary +
"\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}";

if (formFields != null)
{
foreach (string key in formFields.Keys)
{
string formitem = string.Format(formdataTemplate, key, formFields[key]);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
}

string headerTemplate =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";

for (int i = 0; i < files.Length; i++)
{
memStream.Write(boundarybytes, 0, boundarybytes.Length);
var header = string.Format(headerTemplate, "uplTheFile", files);
var headerbytes = System.Text.Encoding.UTF8.GetBytes(header);

memStream.Write(headerbytes, 0, headerbytes.Length);

using (var fileStream = new FileStream(files, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[1024];
var bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
}
}

memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
request.ContentLength = memStream.Length;

using (Stream requestStream = request.GetRequestStream())
{
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
}

using (var response = request.GetResponse())
{
Stream stream2 = response.GetResponseStream();
StreamReader reader2 = new StreamReader(stream2);
return reader2.ReadToEnd();
}
}
private void UploadFile()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "image|*.jpg";
dlg.ShowDialog();

if (dlg.FileName != "")
{
string[] f = new string[1];
f[0] = dlg.FileName;
NameValueCollection formFields = new NameValueCollection();
UploadFilesToRemoteUrl("http://localhost:8080/api/Photo", f, formFields);
}

}
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
10-6-2019 16:34:30
Put json data to server api:


using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Script.Serialization;


try
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:8080/api/Khachhang");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
WebA.KhachhangInfo inf = getInfo();
JavaScriptSerializer jss = new JavaScriptSerializer();
// serialize into json string
string myContent = jss.Serialize(inf);

streamWriter.Write(myContent);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
MessageBox.Show(result);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
11-6-2019 14:4:13
public static void Register(HttpConfiguration config)
{
// Web API configuration and services

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);

//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
}
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
28-10-2019 15:41:50
get api data

[code]
var url = new URL(window.location.href);
var token = url.searchParams.get("token");

$.ajax({
type: "GET",
url: "http://192.168.40.148:3001/api/place/places",
beforeSend: function (xhr) { xhr.setRequestHeader('access_token', token); },
success: function (resp) {
console.log('data ', resp.data);
for (i = 0; i < resp.data.length; i++) {
var li = document.createElement("LI");
link = '<a href="javascript:selectPlace(\'' + resp.data._id + '\', \'' + resp.data.name + '\')">' + resp.data.name + '</a>';
li.innerHTML = link;
document.getElementById("places").appendChild(li);
}
}
});
[/code]
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 
aspnet

Lập trình không biên giới
608  bài
5-2-2020 11:14:23
[code]public string toJsonData(DataTable dt)
{
StringBuilder jsonData = new StringBuilder();
jsonData.Append("[");
for(int i = 0; i < dt.Rows.Count; i++)
{
StringBuilder sRow = new StringBuilder();
sRow.Append("{");
string templ = "\"{0}\":\"{1}\"";
for(int j = 0; j < dt.Columns.Count; j++)
{
sRow.Append(String.Format(templ, dt.Columns[j].ColumnName, dt.Rows[j].ToString()));
if (j < dt.Columns.Count - 1)
sRow.Append(",");
}
sRow.Append("}");

if (i < dt.Rows.Count - 1)
sRow.Append(",");
jsonData.Append(sRow.ToString());
}
jsonData.Append("]");

return jsonData.ToString();
}
[/code]
---
Cây sẽ cho lộc và cây sẽ cho hoa ...
 

Chủ đề gần đây :

Cùng loại :

Tên file Mô tả chi tiết Ngày
NWeb.zip (1) Module đơn giản Newsweb trên Dotnetnuke v10.x.x.x10/18/2025 8:08:11 AM
vspforum.zip (11) Ma nguon vspforum ngay xua4/18/2023 6:38:37 AM
pdfjs.rar (2) pdfjs 2017 : hiển thị tốt trên iphone 11, 12, 13 không lỗi, bản 2012 sẽ lỗi trên iphone6/21/2022 11:52:48 AM
pdfjs2.rar (2) Xem file pdf bằng viewer.hml cua pdfjs (thư viện chuẩn mozilla) 2012. https://mozilla.github.io/pdf.js/getting_started/#download có thể download bản prebuild tại đây6/21/2022 11:52:04 AM
runner.zip (0) using three.js, orbitcontrol to view an object move random on map. Di chuyển 1 đồ vật ngẫu nhiên trên bản đồ, sử dụng với demo nhân viên di chuyển trong văn phòng. Toàn js download về là chạy12/5/2019 5:55:14 PM
gmap.zip (1) google map + marker7/17/2019 2:25:05 PM
vinsmarthomeservice.zip (1) java post json to api, use AsyncTask, event listener7/9/2019 5:00:10 PM
fblogin.zip (0) Login facebook bang javascript SDK7/9/2019 9:16:37 AM
autocomplete-location.zip (2) autocomplete location geo from google place, html + js7/4/2019 4:37:55 PM
WebAPI.zip (8) api for android access db (v1.0.0)7/4/2019 9:14:17 AM