【问题标题】:Unable to redirect on controller from @Html.ActionLink in mvc .net无法从 mvc .net 中的 @Html.ActionLink 在控制器上重定向
【发布时间】:2019-03-06 06:33:14
【问题描述】:

我正在尝试从不同页面登录网站并返回该 URL。但我无法调用我的控制器。

假设: 我正在尝试从此 URL 调用控制器: http://localhost:50208/VendorStore/40/Products/Categories

并声明控制器 http://localhost:50208/VendorStore/Account/ExternalLoginCallback

但是当点击按钮时它会重定向到http://localhost:50208/VendorStore/40/Account/ExternalLoginCallback

登录按钮代码:

 `@Html.ActionLink("Login", "ExternalLoginCallback","Account", new { returnUrl = Request.Url.PathAndQuery }, null) `

控制器代码:

  [AllowAnonymous]
  public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
    {

    }

【问题讨论】:

    标签: c# asp.net-mvc model-view-controller


    【解决方案1】:

    据我了解,您在 URL 中声明了一个区域 VendorStore,如果是,则将其添加到 ActionLink:

    @Html.ActionLink("Login", "ExternalLoginCallback","Account", new { area = "VendorStore", returnUrl = Request.Url.PathAndQuery }, null)
    

    【讨论】:

    【解决方案2】:

    用途:

     @Html.ActionLink("Text", "ActionName", "ControllerName", new { Area = "AreaName" }, null)
    

    注意:第四个参数是传递路由值,如果你传递一个空参数,它会考虑根结构,如果你传递适当的值,它就会将它作为区域。

    也不要忘记使用nullnew{} 作为第5 个参数,因为在创建操作链接时传递nullnew {} 不会重载(文本、操作、控制器、路由数据)或其(text,action,controller,route data,html attribute) 所以使用正确的方法

    在您的项目中:

    @Html.ActionLink("Login", "ExternalLoginCallback", "Account", new { Area = "VendorStore", returnUrl = Request.Url.PathAndQuery  }, null)
    

    【讨论】:

    • 我试过 "@Html.ActionLink("Login", "ExternalLoginCallback", "Account", new { area = "VendorStore", returnUrl = Request.Url.PathAndQuery }, htmlAttributes: new { })" 并得到相同的结果。
    猜你喜欢
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-08-06
    • 2016-10-08
    • 1970-01-01
    相关资源
    最近更新 更多