Is there an equivalent to Response.Redirect("~/Controller/") in Asp.Net Core 2?

Said Roohullah Allem picture Said Roohullah Allem · Jan 7, 2018 · Viewed 7.5k times · Source

Is there an equivalent to Response.Redirect("~/Controller/") in Asp.Net Core 2 ?

I don't want to use ViewComponent. Instead of, I want call a Controller and an action method from a view.

@using Jahan.Blog.Web.Mvc.Models
@{
    ViewBag.Title = "Home Page";
}

<header>
    <h1> Blog </h1>
</header>
<div class="blog-description">
    <p>...</p>
</div>

@{ Response.Redirect("~/Article/");}

Answer

avinash picture avinash · Jan 9, 2018

Try this if within the controller method:

RedirectToAction("yourActionName", "YourControllerName");

or:

Url.Action("YourActionName", "YourControllerName");

This can also be used with parameters as defined in your AppStart -> RouteConfig.cs file

i.e  
routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "YourControllerName", action = "YourActionName", id = 
          UrlParameter.Optional }
      );

to pass parameters simply add new keyword for get method

Url.Action("YourActionName", "YourControllerName", new { id = id });

for Post Method use

Url.Action("YourActionName", "YourControllerName", new { "your variable" = id });