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/");}
So, in brief: Response. Redirect simply tells the browser to visit another page. Server. Transfer helps reduce server requests, keeps the URL the same and, with a little bug-bashing, allows you to transfer the query string and form variables.
RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With