如何在使用 RedirectToAction 时跳过控制器名称?

发布于 2024-11-04 06:55:49 字数 1013 浏览 4 评论 0原文

我有 accountController 类,它有登录和;家庭意见。

    [HandleError]
    public class accountController : Controller
    {
        [HttpPost]
        public ActionResult login(LoginModal model, string returnUrl)
        {
             //Authentication
             return RedirectToAction("home"); 
        }
        public ActionResult home()
        {
             return View(); 
        }
    } 
------------------------------
-----------------------------
Global.asax have Route entry.. so my urls is 
http://lmenaria.com/login
http://lmenaria.com/home

routes.MapRoute(null, "home", new { controller = "account", action = "home" });
routes.MapRoute(null, "login", new { controller = "account", action = "login" });

当我在浏览器上尝试这两个 URL 时,它们工作正常。但是,当登录成功后,它会转到 http://lmenaria.com/account/home 那么我怎样才能从这个网址中删除“帐户”呢?当我使用 return RedirectToAction("home"); 时就会发生这种情况并收到 404 错误。

所以请让我知道如何解决该问题。我不需要 url 中的控制器名称。

谢谢 拉克斯米拉尔·梅纳里亚

I have accountController class and its have login & home views.

    [HandleError]
    public class accountController : Controller
    {
        [HttpPost]
        public ActionResult login(LoginModal model, string returnUrl)
        {
             //Authentication
             return RedirectToAction("home"); 
        }
        public ActionResult home()
        {
             return View(); 
        }
    } 
------------------------------
-----------------------------
Global.asax have Route entry.. so my urls is 
http://lmenaria.com/login
http://lmenaria.com/home

routes.MapRoute(null, "home", new { controller = "account", action = "home" });
routes.MapRoute(null, "login", new { controller = "account", action = "login" });

When I tried the both URL on browser they are working fine. But when login success then its go to http://lmenaria.com/account/home
So how can I remove "account" from this url. this is going when I used return RedirectToAction("home"); and getting 404 error.

So please let me know how can I resolved that issue. I don't need Controller Name in url.

Thanks
Laxmilal Menaria

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱桃奶球 2024-11-11 06:55:49

路线.MapRoute(“家”,“家”,新{控制器=“帐户”,行动=“家”});

我尝试使用上面的 MapRoute &使用 RedirectToRoute 而不是 RedirectTOAction 及其工作。

谢谢。

routes.MapRoute("home", "home", new { controller = "account", action = "home" });

I tried with Above MapRoute & use RedirectToRoute instead of RedirectTOAction and its work.

Thanks.

掩饰不了的爱 2024-11-11 06:55:49

如果你想将控制器默认为“account”,而不在你的URL中显示它,那么你可以执行类似的操作:

routes.MapRoute(null, "{action}", new {controller = "account" });

如果需要,您也可以为操作使用默认值

。routes.MapRoute(null, "{action}", new {controller = "account", action = "home" });


我正在编辑我的答案,让您知道您不需要像您所做的那样显式定义每条路线。 MVC 路由匹配模式。因此,而不是

routes.MapRoute(null, "home", new {controller = "account", action = "home" });
routes.MapRoute(null, "login", new {controller = "account", action = "login" });

仅使用

routes.MapRoute(null, "{action}", new {controller = "account" });

如果您定义了多个模式,请注意路由顺序,因为顺序很重要。 MVC 将使用与您的 URL 匹配的第一个模式。

If you want to default the controller to "account", without showing it in your URL, then you can do something like

routes.MapRoute(null, "{action}", new { controller = "account" });

if you want, you can use a default value for action too

routes.MapRoute(null, "{action}", new { controller = "account", action = "home" });


I'm editing my answer to let you know that you don't need to explicitly define each route as you're doing. MVC routes match patterns. So, instead of

routes.MapRoute(null, "home", new { controller = "account", action = "home" });
routes.MapRoute(null, "login", new { controller = "account", action = "login" });

use just

routes.MapRoute(null, "{action}", new { controller = "account" });

And pay attention to the routes order if you define more than one pattern, because order matters. MVC will use the first pattern that matches your URL.

三生殊途 2024-11-11 06:55:49

我认为真正的问题是您没有创建或删除家庭控制器。

从您的描述来看,您确实应该为 Home 控制器 Index 操作调用 RedirectToAction。

I think the real issue is that you either didn't create or removed the home controller.

From your description it really sounds like you should be calling RedirectToAction for the Home controller Index action.

许你一世情深 2024-11-11 06:55:49

您的登录操作中有 [HttpPost]。这应该是问题所在。我只是运行没有 [HttpPost] 属性的代码。它正在工作。当您输入 http://lmenaria.com/login 时,您的“登录”操作将不会被触发,因为属性。因此,必须有一些其他路由来执行路由。

you have [HttpPost] on your Login action. Which should be the problem. I just run your codes without [HttpPost] attribute. it is working. when you type http://lmenaria.com/login, your "login" action will not be fired, because of the attribute. so, there must be some other routes that does the routing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文