RedirectToAction usage in asp.net mvc

后端 未结 4 434
甜味超标
甜味超标 2020-12-28 13:34

I want to post some questions about ASP.Net MVC. I am not familiar with web developing, But I was assigned to the web part of a project. We are doing the follow

4条回答
  •  臣服心动
    2020-12-28 13:45

    If you don't want the parameter to be shown in the address bar you will need to persist it somewhere on the server between the redirects. A good place to achieve this is TempData. Here's an example:

    public ActionResult Index()
    {
        TempData["nickname"] = Person.nickname;
        return RedirectToAction("profile", "person");
    }
    

    And now on the Profile action you are redirecting to fetch it from TempData:

    public ActionResult Profile()
    {
        var nickname = TempData["nickname"] as string;
        if (nickname == null)
        {
            // nickname was not found in TempData.
            // this usually means that the user directly
            // navigated to /person/profile without passing
            // through the other action which would store
            // the nickname in TempData
            throw new HttpException(404);
        }
        return View();
    }
    

    Under the covers TempData uses Session for storage but it will be automatically evicted after the redirect, so the value could be used only once which is what you need: store, redirect, fetch.

提交回复
热议问题