如何重定向到调用 .net-core MVC 中的另一个操作的视图
How can I redirect to the View which called another action in .net-core MVC
我正在使用 ASP.NET Core 2.2 MVC 开发一个项目。这是一个名为 MyView 的 view,其中有一个 link 调用 MyAction动作。
<div>
<a asp-controller="Home" asp-action="MyAction">my link</a>
</div>
这是 MyAction 执行 st.
public async Task<IActionResult>MyAction(argument1,argument2)
{
if(a condition)
{
it should redirect me to the view which *MyLink* is placed there.
}
else
{
return false;
}
}
我想知道如何找到调用 MyAction 的视图(页面)的 url 并重定向到它。
这是项目结构(我做的很简单)
project structure
I found this. But I want to add NOTHING to the destination action. It doesn't work in .NET Core
How do I redirect to the previous action in ASP.NET MVC?
您可以为每个页面使用标题或特定 ID。在视图中分配 ViewData["Title"] 后(或者您可以在不分配标题的情况下为每个页面使用特定的 ID)
@{
ViewData["Title"] = "MyView";
}
<div>
<a asp-controller="Home" asp-action="MyAction" asp-route-returnUrl="@ViewData["Title"]" >my link</a>
</div>
那你就可以实际使用了
public async Task<IActionResult>MyAction(string returnUrl)
{
return View(returnUrl)
return RedirectToAction(returnUrl,"YourAction")
}
如果你要对提醒字符串做一些不同的事情,请使用 switch case,比如(redirecttoaction 或任何你喜欢的)
switch (returnUrl)
{
case"MyView":
.....
break;
case "YourView":
.....
break;
default:
...
break;
}
我的解决方案
我做到了。以下是如何重定向到调用操作的视图。
Mayak为此提供了很好的解决方案。我只是添加了一些额外的信息以确保安全。正如 he/she 所说,
首先,您应该将标题分配给所有 Views.
然后你应该通过 Path 和 QueryString 通过 asp-route-returnUrl.
@{
ViewData["Title"] = "MyView";
}
<div>
<a asp-controller="Home" asp-action="MyAction" asp-route-returnUrl="@string.Format("{0}{1}",Context.Request.Path, Context.Request.QueryString)" >my link</a>
</div>
然后在MyAction中我们将returnUrl作为参数传递
public async Task<IActionResult>MyAction(string returnUrl)
{
if(everything is alright)
{
return LocalRedirect(returnUrl);
}
else
{
return false;
}
}
有一种叫做开放重定向攻击的东西
https://docs.microsoft.com/en-us/aspnet/core/security/preventing-open-redirects?view=aspnetcore-2.2
当您的应用程序从表单接收数据时。它不安全,数据可能会被篡改以将用户重定向到外部恶意 URL。因此,通过使用 LocalRedirect,您可以防止开放重定向攻击。
我正在使用 ASP.NET Core 2.2 MVC 开发一个项目。这是一个名为 MyView 的 view,其中有一个 link 调用 MyAction动作。
<div>
<a asp-controller="Home" asp-action="MyAction">my link</a>
</div>
这是 MyAction 执行 st.
public async Task<IActionResult>MyAction(argument1,argument2)
{
if(a condition)
{
it should redirect me to the view which *MyLink* is placed there.
}
else
{
return false;
}
}
我想知道如何找到调用 MyAction 的视图(页面)的 url 并重定向到它。
这是项目结构(我做的很简单)
project structure
I found this. But I want to add NOTHING to the destination action. It doesn't work in .NET Core
How do I redirect to the previous action in ASP.NET MVC?
您可以为每个页面使用标题或特定 ID。在视图中分配 ViewData["Title"] 后(或者您可以在不分配标题的情况下为每个页面使用特定的 ID)
@{
ViewData["Title"] = "MyView";
}
<div>
<a asp-controller="Home" asp-action="MyAction" asp-route-returnUrl="@ViewData["Title"]" >my link</a>
</div>
那你就可以实际使用了
public async Task<IActionResult>MyAction(string returnUrl)
{
return View(returnUrl)
return RedirectToAction(returnUrl,"YourAction")
}
如果你要对提醒字符串做一些不同的事情,请使用 switch case,比如(redirecttoaction 或任何你喜欢的)
switch (returnUrl)
{
case"MyView":
.....
break;
case "YourView":
.....
break;
default:
...
break;
}
我的解决方案
我做到了。以下是如何重定向到调用操作的视图。 Mayak为此提供了很好的解决方案。我只是添加了一些额外的信息以确保安全。正如 he/she 所说, 首先,您应该将标题分配给所有 Views.
然后你应该通过 Path 和 QueryString 通过 asp-route-returnUrl.
@{
ViewData["Title"] = "MyView";
}
<div>
<a asp-controller="Home" asp-action="MyAction" asp-route-returnUrl="@string.Format("{0}{1}",Context.Request.Path, Context.Request.QueryString)" >my link</a>
</div>
然后在MyAction中我们将returnUrl作为参数传递
public async Task<IActionResult>MyAction(string returnUrl)
{
if(everything is alright)
{
return LocalRedirect(returnUrl);
}
else
{
return false;
}
}
有一种叫做开放重定向攻击的东西
https://docs.microsoft.com/en-us/aspnet/core/security/preventing-open-redirects?view=aspnetcore-2.2
当您的应用程序从表单接收数据时。它不安全,数据可能会被篡改以将用户重定向到外部恶意 URL。因此,通过使用 LocalRedirect,您可以防止开放重定向攻击。