简体   繁体   中英

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/");}

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 });

You should return a result from your action.

Views should not contain logic like that; you should move all such logic to an action or filter.

您正在从视图重定向... ASP MVC Core 2Razor Pages中的视图无法访问请求的上下文,因此他们无法重定向...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM