다른 컨트롤러에서 인덱스로 리디렉션하는 방법은 무엇입니까?


129

Index다른 컨트롤러에서보기 로 리디렉션하는 방법을 찾으려고 노력했습니다 .

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

이것이 바로 지금 시도한 것입니다. 이제 내가받은 코드에는 ActionLink필요한 페이지로 연결되는 링크가 Redirect있습니다.

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

답변:


272

컨트롤러 이름을 가진 과부하도 사용하십시오 ...

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

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

3
좋아,이 일했다. 나는 전에 그것을 할 때 오타 일이 일찍 시도해야합니다.
cjohnson2136

2

우리 MVC 초보자에게는 이것이 매우 도움이되었습니다. 단순히 다른 컨트롤러가 나타내는 다른 폴더의 다른보기로 리디렉션하면이 내용을 읽을 때까지 나에게 가져 왔습니다.
atconway

컨트롤러없이보기로 리디렉션하는 방법은 무엇입니까? 같은Shared/Error
딜런 Czenski

28

시험:

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


3
귀하의 통지와 관련하여 어떤 이유로 인해 new {}대신 사용 하는 것이 더 낫 null습니까?
musefan

16

다음 코드를 사용할 수 있습니다 :

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

RedirectToAction을 참조하십시오


나는 그것을 시도했지만 작동하지 않았다. 그것은 페이지를 찾을 수 없다는 오류를 주었다
cjohnson2136

"컨트롤러"와 함께 있어야합니다. return RedirectToAction("Index", "Home");
Hiraeth

다른 방법으로는 찾을 수없는 "/ Index"를 사용해야합니다
code4j

@ code4j 기본 경로를 어떻게 정의 했습니까? 제어기 및 조치에 대한 기본값을 추가 했습니까?
Wouter de Kort 2016 년

2

오버로드 방법을 사용할 수 있습니다 RedirectToAction(string actionName, string controllerName);

예:

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

1

로컬 리디렉션을 사용할 수 있습니다. 다음 코드는 HomeController의 색인 페이지를 뛰어 넘습니다.

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

            return View();
        }
}

0

완전한 답변 (.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"작동하지 않습니다!

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.