【问题标题】:How to redirect to Index from another controller?如何从另一个控制器重定向到索引?
【发布时间】:2011-12-15 01:54:47
【问题描述】:

我一直在寻找一些方法来从另一个控制器重定向到 Index 视图。

public ActionResult Index()
{                
     ApplicationController viewModel = new ApplicationController();
     return RedirectToAction("Index", viewModel);
}

这是我现在尝试的。现在给我的代码有一个ActionLink,它也链接到我需要的页面Redirect

@Html.ActionLink("Bally Applications","../Application")

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    使用带有控制器名称的重载...

    return RedirectToAction("Index", "MyController");
    

    @Html.ActionLink("Link Name","Index", "MyController", null, null)
    

    【讨论】:

    • 好的,这行得通。我之前试过这个肯定是我以前做的时候打错了。
    • 这样做会更快,但有一个计时器阻止我
    • 啊,这对我们 MVC 新手来说非常有帮助。我只是简单地重定向到由不同控制器表示的不同文件夹中的另一个视图,直到我读到这个。
    • 如何在没有控制器的情况下重定向到视图?比如Shared/Error
    【解决方案2】:

    尝试:

    public ActionResult Index() {
        return RedirectToAction("actionName");
        // or
        return RedirectToAction("actionName", "controllerName");
        // or
        return RedirectToAction("actionName", "controllerName", new {/* routeValues, for example: */ id = 5 });
    }
    

    .cshtml 视图中:

    @Html.ActionLink("linkText","actionName")
    

    或:

    @Html.ActionLink("linkText","actionName","controllerName")
    

    或:

    @Html.ActionLink("linkText", "actionName", "controllerName", 
        new { /* routeValues forexample: id = 6 or leave blank or use null */ }, 
        new { /* htmlAttributes forexample: @class = "my-class" or leave blank or use null */ })
    

    注意不建议在最终表达式中使用null,最好使用空白new {}而不是null

    【讨论】:

    • 关于你的通知,为什么使用new {}而不是null更好?
    【解决方案3】:

    您可以使用以下代码:

    return RedirectToAction("Index", "Home");
    

    RedirectToAction

    【讨论】:

    • 我试过了,但没有用。它给了我页面找不到错误
    • 应该与“控制器”:return RedirectToAction("Index", "Home");
    • 我需要使用“/Index”否则找不到
    • @code4j 你是如何定义你的默认路由的?您是否为控制器和操作添加了默认值?
    【解决方案4】:

    你可以使用重载方法RedirectToAction(string actionName, string controllerName);

    示例:

    RedirectToAction(nameof(HomeController.Index), "Home");
    

    【讨论】:

      【解决方案5】:

      您可以使用本地重定向。 以下代码在跳转 HomeController 的 Index 页面:

      public class SharedController : Controller
          {
              // GET: /<controller>/
              public IActionResult _Layout(string btnLogout)
              {
                  if (btnLogout != null)
                  {
                      return LocalRedirect("~/Index");
                  }
      
                  return View();
              }
      }
      

      【讨论】:

        【解决方案6】:

        完整答案(.Net Core 3.1)

        这里的大多数答案都是正确的,但有点脱离上下文,所以我将提供一个适用于 Asp.Net Core 3.1 的完整答案。为了完整起见:

        [Route("health")]
        [ApiController]
        public class HealthController : Controller
        {
            [HttpGet("some_health_url")]
            public ActionResult SomeHealthMethod() {}
        }
        
        [Route("v2")]
        [ApiController]
        public class V2Controller : Controller
        {
            [HttpGet("some_url")]
            public ActionResult SomeV2Method()
            {
                return RedirectToAction("SomeHealthMethod", "Health"); // omit "Controller"
            }
        }
        

        如果您尝试使用任何特定于 url 的字符串,例如"some_health_url",不行!

        【讨论】:

          【解决方案7】:

          标签助手:

          <a asp-controller="OtherController" asp-action="Index" class="btn btn-primary"> Back to Other Controller View </a>
          

          在controller.cs中有一个方法:

          public async Task<IActionResult> Index()
          {
              ViewBag.Title = "Titles";
              return View(await Your_Model or Service method);
          }
          

          【讨论】:

            【解决方案8】:

            RedirectToRoute() 是另一种选择。只需将路线作为参数传递。此外,使用 nameof() 可能是更好的约定,因为您没有将控制器名称硬编码为字符串。

             return RedirectToRoute(nameof(HomeController) + nameof(HomeController.Index));
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-10-17
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多