在Asp.Net Core 2中是否有与Response.Redirect(“〜/ Controller /”)等效的东西?

Jah*_*han 4 asp.net asp.net-core asp.net-core-2.0 razor-pages

有相当于Response.Redirect("~/Controller/")in Asp.Net Core 2吗?

我不想用ViewComponent。相反,我想从视图中调用Controller和action方法。

@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/");}
Run Code Online (Sandbox Code Playgroud)

小智 6

如果在controller方法内,请尝试以下操作:

RedirectToAction("yourActionName", "YourControllerName");
Run Code Online (Sandbox Code Playgroud)

要么:

Url.Action("YourActionName", "YourControllerName");
Run Code Online (Sandbox Code Playgroud)

也可以将其与AppStart-> RouteConfig.cs文件中定义的参数一起使用

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

传递参数,只需为get方法添加新关键字

Url.Action("YourActionName", "YourControllerName", new { id = id });
Run Code Online (Sandbox Code Playgroud)

供过帐方法使用

Url.Action("YourActionName", "YourControllerName", new { "your variable" = id });
Run Code Online (Sandbox Code Playgroud)