Sending temporary information between classes with C # 4.0 MVC 4

1

Working with a VS 2015 solution 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 accesses the site "project.site.com" and clicks on an option, I will direct it to the registration site, which is in Project B, but a dropdown will be populated with certain values, and if the user reloads the page, this dropdown should be populated with different values.

Technically this is so, when the user clicks on the given option mentioned above, a method will be triggered in my controller of Project A, which in MVC TempData I enter the value 1, then I call the controller's Index () method of Project B

DESIGN 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 Project B controller's Index () method I will have a validation to customize the values to be populated 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 the TempData comes null. How do I send an information from the OpenCadastro () method, in the controller of Project A, to the method Index (), in the controller of Project B?

I need it to be temporary, that only the OpenCadastro () method sends, and that is not displayed in the URL, so that it is not traceable. I thought of working with Session (so the TempData) or with parameters in methods, however I do not know how to hide the parameter in the URL.

    
asked by anonymous 18.01.2017 / 13:58

3 answers

2

Assuming you have access to both systems, you could perform a ProjectA POST for ProjectB by passing a value. And in projectB you check for this value and fill in your TempData . An example would be something like this:

Project B

Cause an Ation POST to receive data from another application. If it has value, you perform redirect with TempData . It would look 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.

In this way, when updating the page TempData will have null , and thus you do not fill DropDown .

Project A

In your ProjectA you just have to POST to the correct ProjectB page like this:

<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 path of POST ProjectB .

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

    
18.01.2017 / 20:14
2

One option would be to work with cookies .

To be more exact, cookies per domain .

According to the your comment , both systems are subdomains of the same domain, meaning they all end with .site.com.br . So you can set a cookie by clicking the ProjectA link and deleting it in the Action Index of ProjectB .

It would look something like this:

ProjectA

In ProjectA you would create an Action only to create cookie and redirect to ProjectB (you can do via JS as well, it is up to you) :

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() , like this:

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

And in Your ProjectB you just check to see if there is that cookie or not, like this:

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:

18.01.2017 / 20:33
0

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

DESIGN 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 in TempData and redirects 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 inserted an auxiliary method to populate TempData

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

And in the main method I check if it is null or not. Thus, when there is a reload of the page, 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();
}
    
19.01.2017 / 13:03