asp.net - Asp.Net Core 2 中是否有相当于 Response.Redirect ("~/Controller/"的函数?

标签 asp.net asp.net-core asp.net-core-2.0 razor-pages

Asp.Net Core 2 中是否有与 Response.Redirect("~/Controller/") 等效的函数?

我不想使用ViewComponent。相反,我想从 View 中调用 Controller 和操作方法。

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

最佳答案

如果在 Controller 方法中尝试这个:

RedirectToAction("yourActionName", "YourControllerName");

或者:

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

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

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

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

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

用于Post方法使用

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

关于asp.net - Asp.Net Core 2 中是否有相当于 Response.Redirect ("~/Controller/"的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48141038/

相关文章:

c# - 在循环中调用 sqlCommand 增加每一步的执行时间

asp.net - 获取当前ASP.NET Web Api 2操作的URL

asp.net-core - 在 Razor View 中注入(inject) Controller 与注入(inject)服务

c# - azure应用程序服务中的Google应用程序凭据(aspnet core web api)

azure - 我需要将 Firebase 连接到 Azure 通知中心吗?

asp.net - 如何使用自托管 ASP.NET Core 2 应用程序 (httpsys) 进行 HTTPS (SSL)

c# - 从字节[]下载PDF文件

c# - 在 ASP.NET 中获取服务器的 IP 地址?

asp.net - IIS 7经典模式自定义HttpModule中的Set-MaxAge

angular - 发布到 Azure 后的 ENOENT