How to Insert a Parameter from My System on an External URL

0

So, we have a site that when passing the code of a user, it shows the location of the same.

URL example:

http://npaa1215.example.com/gisb_prod/integration/coordUser.aspx?codUser=30071665&zoom=15

In the URL we can pass the parameter codUser .

In my application I have these codes and would like to insert in the field codUser

In the example the site pointing to user codUser=30071665

This is my Barcoviewmodel

    [Key]
    public Guid Id { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "Campo Obrigatório")]
    public bool Ativo { get; set; }

    [Display(Name = "Registro SAP")]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public int SapId { get; set; }

    [Display(Name = "Tancagem Água")]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public int CapacidadeAgua { get; set; }

    [Display(Name = "Tancagem óleo")]
    [Required(ErrorMessage = "Campo Obrigatório")]
    public int CapacidadeOleo { get; set; }


    [Required(ErrorMessage = "Campo Obrigatório")]
    public int Velocidade { get; set; }

    [Required(ErrorMessage = "Preencha o campo E-mail")]
    [MaxLength(100, ErrorMessage = "Máximo {0} caracteres")]
    [EmailAddress(ErrorMessage = "Preencha um E-mail válido")]
    [Display(Name ="E-mail")]
    public string Email { get; set; }

And in my controler I thought about doing something like this:

    string url = http://npaa1215.example.com/gisb_prod/integration/coordUser.aspx?codUser={0}&zoom=15;
    private string GetLocation(BarcoViewModel barcoViewModel)
    {
        return string.Format(url, barcoViewModel.SapId);
    }

But it will not work, would anyone have an example of how I insert this into view ?

How would I do the controller method returns this url with the?

******** EDIT *******

Based on peer response

This is my Controller code:

   http://npaa1215.example.com/gisb_prod/integration/coordUser.aspx?codUser=30071665&zoom=15

   [HttpGet]
    private string GetLocation(BarcoViewModel barcoViewModel)
    {
        return string.Format(url, barcoViewModel);
    }

    [HttpPost]
    public RedirectResult RedirectTo(BarcoViewModel barcoViewModel)
    {
        string destination = GetLocation(barcoViewModel);
        return RedirectPermanent(destination);
    }

This is the View

    <a href="@Url.Action("RedirectTo","Barcos", new { id = item.SapId })" class="btn btn-danger">
     <span title="Excluir" class="glyphicon glyphicon-alert"></span>
    </a>

But even making these additions, I still get the following error:

    
asked by anonymous 18.10.2018 / 16:16

2 answers

0

Create an action on your controller of type RedirectResult

[HttpGet]
public RedirectResult RedirectTo(int id)
{
    string location = GetLocation(id);
    return RedirectPermanent(location);
}

string url = "http://npaa1215.example.com/gisb_prod/integration/coordUser.aspx?codUser={0}&zoom=15";
private string GetLocation(int id)
{
    return string.Format(url, id);
}

And in your View

<a href="@Url.Action("RedirectTo","Barcos", new { id = item.SapId })" class="btn btn-danger">
  <span title="Excluir" class="glyphicon glyphicon-alert"></span>
</a>
    
18.10.2018 / 16:59
0

In a very simple way it can be done directly in JavaScript (You can change the method to receive the id parameter if necessary too.):

  function AbrirUrl() {
    var url = 'http://npaa1215.example.com/gisb_prod/integration/coordUser.aspx?codUser={0}&zoom=15';
    url = url.replace('{0}', '@Model.SapId');

    window.location.href = url;
  }

You can create a method then in the controller where you will treat the URL and return a JSON:

  string url = "http://npaa1215.example.com/gisb_prod/integration/coordUser.aspx?codUser={0}&zoom=15";
  
  [HttpGet]
  private JsonResult GetLocationURL(long SapID)
  {
    return Json(new { URL = string.Format(url, SapID) });
  }

Now in your View just add the pro's request by making it open the URL:

  $.ajax({
    url: '@Url.Action("GetLocationURL", "SEU_CONTROLLER")',
    method: "GET",
    data: { SapID: @Model.SapId },
    success: function (result) {
      window.location.href = result.URL;
    }
  });
    
18.10.2018 / 16:37