如何在MVC中使用RedirectToRoute("routeName")? [英] How to use RedirectToRoute("routeName") in MVC?

查看:219
本文介绍了如何在MVC中使用RedirectToRoute("routeName")?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是对为什么我的RedirectToRoute()方法不起作用感到困惑.我有一个像这样的RouteConfig.cs文件

I am just puzzled on why is my RedirectToRoute() method not working. I have a RouteConfig.cs file like this

routes.MapRoute(
    "pattern1",
    "{action}",
    new { controller = "Home", action = "About" }
);

routes.MapRoute(
    "pattern2",
    "{controller}/{action}/{id}",
    new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);

在此配置上,我的默认控制器 Home 和操作关于被调用,现在在操作方法中,我使用以下值调用RedirectToRoute()

on this configuration my default controller Home and action About is getting called, now in the action method I am calling RedirectToRoute() with the following value like this

public ActionResult Index()
{
    return View();
}
public ActionResult About()
{
    return RedirectToRoute("pattern2");
}

为什么RedirectToRoute()不调用Admin/Index操作

Why is the RedirectToRoute() not calling Admin/Index action

推荐答案

尝试一下:

return RedirectToRoute(new 
{ 
    controller = "Admin", 
    action = "Index", 
    id = null 
});

您还可以使用RedirectToAction()方法.似乎更直观.

You could also use RedirectToAction() method. It seems more intuitive.

这篇关于如何在MVC中使用RedirectToRoute("routeName")?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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