在asp.net的MVC RedirectToAction使用 [英] RedirectToAction usage in asp.net mvc

查看:143
本文介绍了在asp.net的MVC RedirectToAction使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发布有关若干问题ASP.Net MVC 。我不熟悉的网络发展,但我被分配到一个项目的网络的一部分。我们正在做以下几点:第一,我们创建 GET &安培; 此人数据集属性:

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 following: first, we create get & set properties for the person data:

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

和登录后,我们把数据中的一类对象,我们使用 RedirectToAction 这样的:

and after login, we put the data in a class Person object and we use RedirectToAction like this:

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

它的工作正常,但参数在URL中。如何隐藏他们,同时也
我可以隐藏的动作叫什么名字?指导我一些例子以正确的方式,请。

It's working normally, but the parameter are shown in the URL. How can I hide them and also can I hide the action name? Guide me the right way with some examples, please.

推荐答案

参数显示在URL,因为这是第三个参数为 RedirectToAction 是 - 航线值。

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

缺省路由是 {控制器} / {行动} / {ID}

所以这code:

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

将产生以下网址/路径:

Will produce the following URL/route:

/人/资料/ 123

如果你想有一个更清洁的路线,像这样(例如):

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

/人/ 123

创建一个新的路径:

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

和您的网址应该是干净的,像上面。

And your URL should be clean, like the above.

另外,你可以不喜欢用ID可言,你可以使用一些其他的唯一标识 - 就像一个昵称

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

因此​​,URL可以是这样的:

So the URL could be like this:

人/ rpm1984

要做到这一点,只需更改路线:

To do that, just change your route:

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

和动作方法:

public ActionResult Profile(string nickname)
{

}

和您的RedirectToAction code:

And your RedirectToAction code:

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

那是你的后?

这篇关于在asp.net的MVC RedirectToAction使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆