RedirectToAction usage in asp.net mvc

后端 未结 4 427
甜味超标
甜味超标 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.

    0 讨论(0)
  • 2020-12-28 13:51

    this may be solution of problem when TempData gone after refresh the page :-

    when first time you get TempData in action method set it in a ViewData & write check as below:

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

    now on the Profile action :

    public ActionResult Profile()
    {
        var nickname = TempData["nickname"] as string;
    
        if(nickname !=null)
            ViewData["nickname"]=nickname;
    
        if (nickname == null && ViewData["nickname"]==null)
        {
            throw new HttpException(404);
        }
        else
        {
            if(nickname == null)
                nickname=ViewData["nickname"];
        }
    
        return View();
    }
    
    0 讨论(0)
  • 2020-12-28 13:57

    Temp data is capable of handling single subsequent request. Hence, value gone after refresh the page. To mitigate this issue, we can use Session variable also in this case. Try below:

    public ActionResult Index(Person _person)
    {
        Session["personNickName"] = _person.nickName;
        return RedirectToAction("profile", "person");
    }
    

    And in "profile" actionmethod:

    public ActionResult profile()
    {
        Person nickName=(Person)Session["personNickName"];
        if(nickName !=null)
        {
        //Do the logic with the nickName
        }
    }
    
    0 讨论(0)
  • 2020-12-28 14:06

    The parameter are shown in the URL because that is what the third parameter to RedirectToAction is - the route values.

    The default route is {controller}/{action}/{id}

    So this code:

    return RedirectToAction("profile","person",new { personID = Person.personID});
    

    Will produce the following URL/route:

    /Person/Profile/123

    If you want a cleaner route, like this (for example):

    /people/123

    Create a new route:

    routes.MapRoute("PersonCleanRoute",
                    "people/{id}",
                    new {controller = "Person", action = "Profile"});
    

    And your URL should be clean, like the above.

    Alternatively, you may not like to use ID at all, you can use some other unique identifier - like a nickname.

    So the URL could be like this:

    people/rpm1984

    To do that, just change your route:

    routes.MapRoute("PersonCleanRoute",
                        "people/{nickname}",
                        new {controller = "Person", action = "Profile"});
    

    And your action method:

    public ActionResult Profile(string nickname)
    {
    
    }
    

    And your RedirectToAction code:

    return RedirectToAction("profile","person",new { nickname = Person.nickname});
    

    Is that what your after?

    0 讨论(0)
提交回复
热议问题