Doubt with EnableCors asp.net web.api

1

When you are done releasing an address using EnableCors can you use Route or do you need to change anything? is this correct?

[HttpGet]
[Route("consulta/BuscaTipoPagamento")]
[EnableCors(origins: "http://localhost:50907", headers: "*", methods: "*")]
public HttpResponseMessage BuscaTipoPagamento()
{
       try
          {
              var tTabela = new TipoPagamentoAplicacao();
              var listar = tTabela.ListaTodos();
              return Request.CreateResponse(HttpStatusCode.OK, 
                    new { pagamento = listar.ToArray() });
          }
          catch (Exception ex)
          {
             return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
          }
}
    
asked by anonymous 02.01.2017 / 15:07

1 answer

0

By the statement is correct. Whereas you have the Nuget Install-Package Microsoft.AspNet.WebApi.Cors installed:

Register in your config

using System.Web.Http;
namespace WebService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {                
            config.EnableCors();

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

Enable CORS in action, controller, or global (for all controllers), in your case for an action only.

public class ItemsController : ApiController
{
    public HttpResponseMessage GetAll() { }
    [EnableCors(origins: "http://www.example.com", headers: "*", methods: "*")]
    public HttpResponseMessage GetItem(int id) {  }
    public HttpResponseMessage Post() { ... }
    public HttpResponseMessage PutItem(int id) {  }
}

link

    
03.01.2017 / 13:27