How to redirect to Index from another controller?

后端 未结 7 1566
滥情空心
滥情空心 2020-12-02 11:06

I have been looking through trying to find some way to redirect to an Index view from another controller.

public ActionResult Index()
{                  


        
相关标签:
7条回答
  • 2020-12-02 11:37

    Complete answer (.Net Core 3.1)

    Most answers here are correct but taken a bit out of context, so I will provide a full-fledged answer which works for Asp.Net Core 3.1. For completeness' sake:

    [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"
        }
    }
    

    If you try to use any of the url-specific strings, e.g. "some_health_url", it will not work!

    0 讨论(0)
提交回复
热议问题