How to Insert a parameter from my system into an external URL

Asked

Viewed 226 times

0

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

Example of the URL:

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 codUser field

In the example the site pointing to the 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 on my controller I thought I’d do 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 won’t work, someone would have an example of how I insert this into view?

As I would for the method of controller return that url with the parameter?

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

Based on colleagues' response

That’s 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>

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

inserir a descrição da imagem aqui

  • The Url you want to return is from your own application?

  • What exactly are you trying to do? redirect to this address or simply return the string?

  • @Victorlaio this url is from another system, it is already running on the internet, my idea is to insert the"Sapid" of my viewmodel, in the field "codUser". then the user when clicking the button, for example it is redirected to this site, only with the parameter q I passed.

  • @Leandroangelo redirect to this site, only passing in the field "codeUser" the property "Sapid" of my system. Ai when the user clicks on a button, for example, it redirects the user to that site.

  • I put 2 possible solutions for you friend.

2 answers

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 request to the method 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;
    }
  });

  • I understood, but what if I was going to pass via url.action. An example: <a href="@Url.Action("Getlocationurl", "Boats", new {id = item.Sapid})" class="btn btn-Primary"> <span title="Details" class="glyphicon glyphicon-search"></> </a> My idea is to put a button in the table that lists users, to redirect the user to this url

  • You can’t direct the browser to an external URL directly from the Controller as far as I know.

  • Blz, to try to implement json But it seems that you can’t access @Model inside the script, it seems that it doesn’t find the Sapid parameter, in fact it doesn’t find any model parameters Ienumerable<BR.Rve.Application.ViewModels.Barcoviewmodel>

  • So, it will only access @Model if the script is in cshtml... Pq does not use the simple js method that posted it in the first?

0


Create an action in your controller like 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>
  • With that RedirectPermanent() can I place Urls that are not in my app? For example: Redirectpermanent("http://www.google.com")?

  • Yes, you can do it.

  • @Leandroangelo I get it. I made the suggestion and added the code in the view: href="@Url.Action("Redirectto","Boats", new { id = item.Sapid })" But the default error: "Server Error in Application '/'", any suggestions?

  • @Leandroangelo ok, I’ll edit the question and show you how to do it, including in the view

  • @Leandroangelo Ready, I edited the question with the changes and with the error.

  • @Leandroangelo Vi, thank you for the feedbacks. The "Return string.Format(url, id)" returns the following error: "The input string was not in a correct format. '" Would you know what could be?

  • @Jhensen Debug and see where you went wrong

  • @Leandroangelo Resolvi, thanks for the help, I will mark as answer.

  • @Jhensen Resolveu?

  • @Leandroangelo Solved yes, is redirected to the site, replacing the code as I wanted. It was my mistake, your code is correct.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.