Skip to content

Latest commit

 

History

History
217 lines (154 loc) · 4.23 KB

READMECEP.md

File metadata and controls

217 lines (154 loc) · 4.23 KB

Canducci CEP

Web Service http://viacep.com.br/

Canducci CEP

Informações Pacote Canducci CEP

Version Twitter

Instalação do Pacote (NUGET)

PM> Install-Package CanducciCep

Versão >= 4.0.0

Como utilizar?

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

Como utilizar?

Declare o namespace using Canducci.Zip;

Busca das informações pelo CEP informado?

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;
}

Busca de vários CEP informando UF, Cidade e Endereço?

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;
}

Exemplo MVC ASP.NET (com async, await e Task)

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();

        }

Lista de Unidade Federativas do Brasil

Dictionary<object, object> ufs = ZipCodeLoad.UfToList();

Verificação de dados válidos para buscas de CEP e Endereços

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))
{

}