Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/");}
like image 589
Roohi Avatar asked Jan 07 '18 20:01

Roohi


People also ask

What is the difference between server transfer and response redirect?

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.

Which code statement is used to redirect to a controller action in a different area?

RedirectToAction(String, String) Redirects to the specified action using the action name and controller name.

What is the difference between redirect and RedirectToAction in MVC?

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.


1 Answers

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 });
like image 147
avinash Avatar answered Sep 23 '22 21:09

avinash