简体   繁体   English

RedirectToAction 在 asp.net mvc 中的用法

[英]RedirectToAction usage in asp.net mvc

I want to post some questions about ASP.Net MVC .我想发布一些关于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 following: first, we create get & set properties for the person data:我们正在执行以下操作:首先,我们为人员数据创建getset属性:

public class Person
{
    public int personID {get;set;}
    public string personName {get;set;}
    public string nric {get;set;}
}

and after login, we put the data in a class Person object and we use RedirectToAction like this:登录后,我们将数据放入类Person对象中,并像这样使用RedirectToAction

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

It's working normally, but the parameter are shown in the URL.它工作正常,但参数显示在 URL 中。 How can I hide them and also can I hide the action name?如何隐藏它们以及隐藏动作名称? Guide me the right way with some examples, please.请用一些例子指导我正确的方法。

The parameter are shown in the URL because that is what the third parameter to RedirectToAction is - the route values.该参数显示在 URL 中,因为这是RedirectToAction的第三个参数 - 路由值。

The default route is {controller}/{action}/{id}默认路由是{controller}/{action}/{id}

So this code:所以这段代码:

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

Will produce the following URL/route:将产生以下 URL/路由:

/Person/Profile/123 /人/简介/123

If you want a cleaner route, like this (for example):如果你想要一条更干净的路线,就像这样(例如):

/people/123 /人/123

Create a new route:创建新路线:

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

And your URL should be clean, like the above.并且您的 URL 应该像上面那样干净。

Alternatively, you may not like to use ID at all, you can use some other unique identifier - like a nickname.或者,您可能根本不喜欢使用 ID,您可以使用其他一些唯一标识符——比如昵称。

So the URL could be like this:所以 URL 可能是这样的:

people/rpm1984人/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:以及您的 RedirectToAction 代码:

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

Is that what your after?那是你的追求吗?

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 .实现此目的的好地方是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 :现在在 Profile 操作上,您要重定向以从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.在幕后, TempData使用Session进行存储,但它会在重定向后自动退出,因此该值只能使用一次,这正是您需要的:存储、重定向、获取。

this may be solution of problem when TempData gone after refresh the page:-当刷新页面后 TempData 消失时,这可能是问题的解决方案:-

when first time you get TempData in action method set it in a ViewData & write check as below:当你第一次在 action 方法中获取 TempData 时,将其设置在 ViewData 中并写入检查,如下所示:

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();
}

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.为了缓解这个问题,我们也可以在这种情况下使用 Session 变量。 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM