[英]How can I redirect to the View which called another action in .net-core MVC
Im developing a project with ASP.NET Core 2.2 MVC. 我正在使用ASP.NET Core 2.2 MVC开发项目。 Here is a view named MyView in which there is a link which calls MyAction action.
这是一个名为MyView的视图 ,其中有一个调用MyAction操作的链接。
<div>
<a asp-controller="Home" asp-action="MyAction">my link</a>
</div>
And this is MyAction which does st. 这是MyAction ,它可以
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;
}
}
I want to know how can I find the url of the view(page) which called MyAction and redirect to it. 我想知道如何找到名为MyAction的视图(页面)的URL并重定向到它。
This is project structure(I made it simple) 这是项目结构(我简化了)
I found this.
我找到了这个。 But I want to add NOTHING to the destination action.
但我想在目标操作中添加NOTHING。 It doesn't work in .NET Core
在.NET Core中不起作用
How do I redirect to the previous action in ASP.NET MVC?如何重定向到ASP.NET MVC中的上一个操作?
You can use title or specific id for every page. 您可以为每个页面使用标题或特定ID。 After you assigned ViewData["Title"] in the view (or you can use specific id for everypage without assigning title)
在视图中分配ViewData [“ Title”]之后(或者您可以为每个页面使用特定的ID,而无需分配标题)
@{
ViewData["Title"] = "MyView";
}
<div>
<a asp-controller="Home" asp-action="MyAction" asp-route-returnUrl="@ViewData["Title"]" >my link</a>
</div>
Then you can use it in action 然后就可以使用它了
public async Task<IActionResult>MyAction(string returnUrl)
{
return View(returnUrl)
return RedirectToAction(returnUrl,"YourAction")
}
If you are gonna do something different use switch case for the reminder string like (redirecttoaction or whatever you like) 如果您要对提醒字符串使用不同的用法(例如,redirecttoaction或其他您喜欢的方式),请使用大小写切换
switch (returnUrl)
{
case"MyView":
.....
break;
case "YourView":
.....
break;
default:
...
break;
}
I did it. 我做的。 Here is how you can redirect to the view which called the action.
这是您如何重定向到调用该操作的视图。 Mayak provided a good solution for it.
Mayak为此提供了一个很好的解决方案。 I just added some extra information to make it secure.
我只是添加了一些额外的信息以使其安全。 As he/she said, first,you should assign Title to all Views .
如他/她所说,首先,您应该为所有视图分配标题。
And after that you should pass Path and QueryString Via asp-route-returnUrl. 然后,您应该通过asp-route-returnUrl传递Path和QueryString 。
@{
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>
Then in MyAction we pass the returnUrl as an argument 然后在MyAction中 ,将returnUrl作为参数传递
public async Task<IActionResult>MyAction(string returnUrl)
{
if(everything is alright)
{
return LocalRedirect(returnUrl);
}
else
{
return false;
}
}
There is something called open redirect attack 有一种叫做开放重定向攻击的东西
https://docs.microsoft.com/en-us/aspnet/core/security/preventing-open-redirects?view=aspnetcore-2.2 https://docs.microsoft.com/zh-cn/aspnet/core/security/preventing-open-redirects?view=aspnetcore-2.2
When your application receives data from a form. 当您的应用程序从表单接收数据时。 It's not secure and data can potentially be tampered with to redirect users to an external, malicious URL.
它不安全,并且可能会篡改数据以将用户重定向到外部恶意URL。 So by using LocalRedirect you can prevent open redirect attacks.
因此,通过使用LocalRedirect,可以防止开放重定向攻击。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.