Web Service http://viacep.com.br/
PM> Install-Package CanducciCep
Versão >= 4.0.0
Declare o namespace using Canducci.Zip;
[HttpPost]
public async Task<IActionResult> Cep(string cep)
{
ZipCodeResult result = await zip.FindAsync(cep);
ViewData["cep"] = cep;
if (result.IsValid)
{
return View(result.Result);
}
return View();
}
ou
[HttpPost]
public async Task<IActionResult> Cep(string cep)
{
ZipCode zipCode = null;
if (ZipCode.TryParse(cep, out zipCode))
{
ZipCodeResult result = await zip.FindAsync(zipCode);
ViewData["cep"] = cep;
if (result.IsValid)
{
return View(result.Result);
}
}
return View();
}
Versão < 4.0.0
Declare o namespace using Canducci.Zip;
try
{
//Observação
//Formato válido para o CEP: 01414000 ou 01414-000 ou 01.414-000
ZipCodeLoad zipLoad = new ZipCodeLoad();
ZipCode zipCode = null;
if (ZipCode.TryParse("01414000", out zipCode))
{
ZipCodeInfo zipCodeInfo = zipLoad.Find(zipCode);
}
}
catch (ZipCodeException ex)
{
throw ex;
}
try
{
//Observações
//Cidade no minimo 3 letras
//Endereço no minimo 3 letras
ZipCodeLoad zipLoad = new ZipCodeLoad();
AddressCode addressCode = null;
if (AddressCode.TryParse(ZipCodeUf.SP, "SÃO PAULO", "AVE", out addressCode))
{
ZipCodeInfo[] zipCodeInfos = zipLoad.Address(addressCode);
}
}
catch (ZipCodeException ex)
{
throw ex;
}
using System.Web.Mvc;
using Canducci.Zip;
using System.Threading.Tasks;
namespace Canducci.Web4._5._1.Controllers
{
public class HomeController : Controller
{
public readonly ZipCodeLoad load;
public HomeController()
{
load = new ZipCodeLoad();
}
protected override void Dispose(bool disposing)
{
if (load != null) load.Dispose();
base.Dispose(disposing);
}
//com Async, Await e Task
public async Task<ActionResult> BuscaComAsync()
{
ZipCodeInfo _info = await load.FindAsync("01414000");
ZipCodeInfo[] _infos = await load.AddressAsync(ZipCodeUf.SP, "SÃO PAULO", "AVE");
return View();
}
//comum
public ActionResult BuscaNormal()
{
ZipCodeInfo _info = load.Find("01414000");
ZipCodeInfo[] _infos = load.Address(ZipCodeUf.SP, "SÃO PAULO", "AVE");
return View();
}
Dictionary<object, object> ufs = ZipCodeLoad.UfToList();
CEP
- Parse
ZipCodeLoad zipLoad = new ZipCodeLoad();
ZipCode zipCode = ZipCode.Parse("01.414-000");
ZipCodeInfo _info = zipLoad.Find(zipCode);
- TryParse
ZipCode zipCode = null;
if (ZipCode.TryParse("01414000", out zipCode))
{
}
Endereços
- Parse
ZipCodeLoad zipLoad = new ZipCodeLoad();
AddressCode addressCode = AddressCode.Parse(ZipCodeUf.SP, "SÃO PAULO", "AVE");
ZipCodeInfo[] _infos = zipLoad.Address(addressCode);
- TryParse
AddressCode addressCode = null;
if (AddressCode.TryParse(ZipCodeUf.SP, "SÃO PAULO", "AVE", out addressCode))
{
}