Sending temporary information between classes with C# 4.0 MVC 4

Asked

Viewed 519 times

1

Working with a Solution VS 2015 that has 2 sites, "projectA.site.com.br" and "projectB.site.com.br", their views and controllers are in the same project, named as "WEB", developed in C# . NET 4 with MVC 4, but in two folders, "Project A" and "Project B", that is, everything is organized.

The situation is as follows, when the user access the site "projectA.site.com.br" and click on a certain option, I will direct it to the registration site, which is in Project B, but a dropdown will be filled with certain values, and if the user reloads the page, this dropdown must be filled with other different values.

Technically, when the user clicks on a certain option, mentioned above, a method will be triggered in my Project A controller, which in MVC Tempdata I enter the value 1, then call the Index() method of the Project B controller

PROJECT A

VIEW

<li>
          <a href="@Url.Action(MVC.ProjetoA.Home.AbrirCadastro())"><i class="fa fa-file-text-o"></i> Cadastro </a>
</li>

CONTROLLER

    [RequiresAuthorization]
    public virtual ActionResult AbrirCadastro()
    {
        Client dadosUsuario = ViewBag.User;
        TempData["Teste"] = 1;
        return Redirect(Url.SubdomainUrl(SubdomainConstants.Register, MVC.ProjetoB.Home.Index()));
    }

In the Index() method of the Project B controller I will have a validation to customize the values to be filled in the dropdown.

PROJECT B

CONTROLLER

    [HttpGet]
    public virtual ActionResult Index()
    {
        if (TempData["Teste"] != null)
        {
            //Preencho dropdown de uma maneira
        }
        else
        {
            //Preencho dropdown de outra maneira
        }
        return View();
    }

But in my Index() method Tempdata is null. How do I send information from the Open Register() method, in the Project A controller, to the Index() method, in the Project B controller?

I need it to be something temporary, that only the Open Registration() method send, and that is not displayed in the URL, so that it is not traceable. I thought about working with Session (that’s why Tempdata) or with parameters in methods, but I don’t know how to hide the parameter in the URL.

  • The question here is not whether you intend to make this "exchange" of information between two classes. You need to think of your project as an application, you need to explain to us which flow this way. The Project A and the Project B are what? Websites? Webservices?

  • They are two sites that access via URL, for example, the project to "projectoa.site.com.br" and project B "projetob.site.com.br". The user will log into the project website b, click on a menu option, then need to send a temporary data of the proejto class b to the project site index a. If the project receives a new request, the temporary data should not exist.

  • You will have to do a POST sent this data or something... This comment already helps a lot in understanding the question, but it is still a little difficult to understand what and how you intend to do.

  • In what is written to access classes, summary "CAN’T".

  • @Felipenegro, it doesn’t matter if you’re in it Solution. If they are different applications, they are different applications and that’s it. You’re going to have to use the resources available to make this communication. Of course, depending on the case, there may be direct communication, but from what you explained, this is the right way. Feel free to [Dit] the question and help understanding. At first that’s right, will have to send the information via HTTP.

  • @jbueno I’ll try to make it clear. The user will enter the site "projectB.site.com.br" with his account, there will be in the menu an option that he clicking on this option should open the site "projectA.site.com.br" with a dropdown filled with information 1, 2 and 3, if he gives a F5 in the browser, will open the same site, but the dropdown filled with 5, 6 and 7, basically.

  • Improved a little more, but it’s still a little confused. I keep what I said, will have to send the information via HTTP.

  • @jbueno Rsrs. Is there any article I can study how to do what you propose, please?

  • @Felipenegro I don’t know if I understand this correctly, but have you ever thought about passing this value by queryString, for example: projectA.site.com.br/Home/Index/value-here and after loading the page just remove queryString. So when updating the value will be different.

  • Without understanding what you need, it is difficult to give suggestions, as they may not suit your context

  • @And how could I do that?

  • I edited the question to make it clearer

  • Both of you are in the same dolmen?

Show 9 more comments

3 answers

2

Assuming you have access to both systems, you could perform a POST from Projectoa to the Projectob passing a value. And, in the projectB you check if this value exists and fill your TempData. An example would be more or less that:

Project B

Create a POST Action to receive data from another application. If you have value, you redirect with the TempData. It would be something like this:

   // GET: ProjetoB
    public ActionResult Index()
    {
        string valor = "";
        if (TempData["Teste"] != null)
        {
            valor = "sim";
        }
        else
        {
            valor = "não";
        }

        ViewBag.Valor = valor;
        return View();
    }

    [AllowAnonymous]
    [HttpPost]
    public ActionResult Index(string valor)
    {
        //Caso tenha valor, você preenche o TempData
        if(!string.IsNullOrEmpty(valor))
            TempData["Teste"] = 1;

        return RedirectToAction("Index");
    }

I am passing the value in case you want to use it in any query. But, for your Model this would be irrelevant.

This way, when updating the page the TempData will be null, and this way you don’t fill the DropDown.

Project A

In his Projectoa just perform the POST to the correct page of Projectob, in this way:

<li>
    @*Na Action você deve passar o valor completo do Action POST do ProjetoB*@
    <form action="/ProjetoB/Index" method="post" id="link-cadastro">
        <input type="hidden" value="true" name="valor" />
        <a href="javascript:void(0);" onclick="$('#link-cadastro').submit()"><i class="fa fa-file-text-o"></i> Cadastro </a>
    </form>
</li>

Change the action="/ProjetoB/Index" to the way of POST of Projectob.

Note that I am only using HTML for a better understanding. However, you can use HtmlHelpers normally.

  • With this suggestion and some differences, I got what I needed, I will post as it was here.

  • In the suggestion you proposed to insert into Project A, how would it look if I needed to pass a Hidden parameter?

2

One option would be to work with cookies.

To be exact, with domain cookies.

According to the your comment, both systems are subdomains of the same domain, that is, they all end with .site.com.br. Therefore, you can set a cookie by clicking on the Projectoa and delete from the Action Index Projectob.

It would be something like this:

Projectoa

In the Projectoa you would create an Action just to create the cookie and redirect to the Projectob (can do via JS too, is at your discretion), in this way:

public ActionResult LinkFake()
{
    HttpCookie cookie = new HttpCookie("MeuCookie");
    //Define o valor do cookie
    cookie.Value = "valorQualquer";
    //Define o Dominio do cookie
    cookie.Domain = "seudominio.com";
    //Adiciona o cookie
    Response.Cookies.Add(cookie);

    //Redireciona para o ProjetoB
    return RedirectToAction("URL PROJETO B");
}

And in your View, you’d call Action LinkFake(), in this way:

<li>
    <a href="@Url.Action(LINK DA ACTIONFAKE AQUI)"><i class="fa fa-file-text-o"></i> Cadastro </a>
</li>

And in His Projectob you just check if there’s that cookie or not, in this way:

public ActionResult Index()
{
    HttpCookie cookie = Request.Cookies["MeuCookie"];

    if(cookie != null)
    {
        cookie.Expires = DateTime.Now.AddDays(-1d);
        Response.Cookies.Add(cookie);

        //Preencho dropdown de uma maneira
    }
    else
    {
        //Preencho dropdown de outra maneira
    }

    return View();
}

Study links:

  • It didn’t work, when I get Cookie’s value back, it doesn’t exist, it’s null.

  • @Felipenegro Both are in the same domain?

  • As far as I know, they are. I can only call my method in Project B as follows "Return Redirect(Url.Subdomainurl(Subdomainconstants.Register, MVC.ProjetoA.Home.Method(Parameter)));"

  • @Felipenegro This way of calling the url’s is very "different". There is no way to know what is in these values. But, if it is siteA.site.com.br and siteB.site.com.br whereas site.com.br should be equal, are subdomains.

  • It was the only way I could call on Project A, the method in Project B. I published the final result. Thank you very much for all your help!!!

0

With the help of @Randrade suggestions, I was able to reach the final result as below:

PROJECT A

The view remains the same as the question.

VIEW

<li>
      <a href="@Url.Action(MVC.ProjetoA.Home.AbrirCadastro())"><i class="fa fa-file-text-o"></i> Cadastro </a>
</li>

The Controller is no longer with Tempdata and I redirect to an auxiliary method in Project B.

CONTROLLER

[RequiresAuthorization]
public virtual ActionResult AbrirCadastro()
{
    Client dadosUsuario = ViewBag.User;
    return Redirect(Url.SubdomainUrl(SubdomainConstants.Register, MVC.ProjetoB.Home.IndexAuxiliar()));
}

PROJECT B

CONTROLLER

I entered an auxiliary method to fill Tempdata

    [HttpGet]
    public virtual ActionResult IndexAuxiliar()
    {
        TempData["Teste"] = true;
        return RedirectToAction(MVC.ProjetoB.Home.Index());
    }

And in the main method I check whether it is null or not. So, when there is a page re-load, Tempdata will be null, thus achieving my goal.

[HttpGet]
public virtual ActionResult Index()
{
    if (TempData["Teste"] != null)
    {
        //Preencho dropdown de uma maneira
    }
    else
    {
        //Preencho dropdown de outra maneira
    }
    return View();
}
  • I made my example with POST only pq with GET if someone accesses Indexauxiliar by Url, will be directed in the same way.

Browser other questions tagged

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