简体   繁体   English

如何重定向到在.net-core MVC中调用了另一个操作的View

[英]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) 这是项目结构(我简化了)

project structure 项目结构

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

My solution 我的解决方案

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传递PathQueryString

@{
     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.

相关问题 如何刷新.net-core mvc项目中的局部视图 - How to refresh partial view in a .net-core mvc project 如何从ASP.NET MVC中的同一操作重定向到另一个视图或外部站点? - How can I redirect to either another view or an external site from the same action in ASP.NET MVC? .NET-Core 3 MVC 控制器 Url.Action 返回 null - .NET-Core 3 MVC Controller Url.Action returning null 如何在.NET-Core Mvc中处理两条路由并定向到一个动作 - How to handle two route and direct to one Action in .NET-Core Mvc WPF(.NET-Core):如何以编程方式将按钮背景颜色绑定到矩阵的索引值? - WPF(.NET-Core):How can I bind a buttons background color, to a matrix's indexed value progmatically? 将字符串数组发布到.net-core mvc - Posting array of strings to .net-core mvc 如何在ASP.Net MVC中找到哪个视图称为哪个局部视图 - How can you find which view called which partial view in ASP.Net MVC 如何从MVC4中的异步动作重定向到另一个动作 - How can I redirect to another action from my Asynchronous Action in MVC4 如何将视图重定向到主页 - ASP.NET Core MVC - How to redirect view to homepage - ASP.NET Core MVC Asp.Net Core重定向到操作但不允许直接调用操作? - Asp.Net Core redirect to action but not allow action to be called directly?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM