简体   繁体   English

无法在asp.net mvc Controller中获取returnurl

[英]Unable to fetch returnurl in asp.net mvc Controller

I have two controllers Base and Login. 我有两个控制器Base和Login。

Base Controller: 基础控制器:

public ActionResult start()
    {
       string action = Request.QueryString[WSFederationConstants.Parameters.Action];
    }

Login Controller: 登录控制器:

 public ActionResult Login(string user,string password,string returnUrl)
    {
        if (FormsAuthentication.Authenticate(user, password))
        {

            if (string.IsNullOrEmpty(returnUrl) && Request.UrlReferrer != null)
                returnUrl = Server.UrlEncode(Request.UrlReferrer.PathAndQuery);

           return RedirectToAction("Start","Base", returnUrl });
        }
        return View();
    }

After authentication is done it gets redirected to Start action in Base Controller as expected. 身份验证完成后,它会按预期重定向到Base Controller中的Start操作。 But the querystring doesnot fetch the value. 但是查询字符串不能获取值。 When hovered over the querystring it shows length value but not the uri. 当悬停在查询字符串上时,它显示长度值,但不显示uri。

How to use the url sent from Login controller in Base Controller and fetch parameters from it? 如何使用从Base Controller中的Login控制器发送的url并从中获取参数?

You are actually returning a 302 to the client. 您实际上是将302返回给客户端。 From the docs . 来自文档

Returns an HTTP 302 response to the browser, which causes the browser to make a GET request to the specified action. 返回对浏览器的HTTP 302响应,这会导致浏览器向指定的操作发出GET请求。

When doing that the client will make another request with the url that you created. 这样做时,客户端将使用您创建的URL发出另一个请求。 In your case something like youruri.org/Base/Start . 在你的情况下像youruri.org/Base/Start Take a look at the network tab in your browser (F12 in Chrome). 查看浏览器中的网络选项卡(Chrome中的F12)。

What I think you want to do is: 我认为你想做的是:

return RedirectToAction
  ("Start", "Base", new { WSFederationConstants.Parameters.Action = returnUrl  });

Assuming that WSFederationConstants.Parameters.Action is a constant. 假设WSFederationConstants.Parameters.Action是常量。 If WSFederationConstants.Parameters.Action returns the string fooUrl your action will return the following to the browser: 如果WSFederationConstants.Parameters.Action返回字符串fooUrl您的操作将向浏览器返回以下内容:

Location:/Base/Start?fooUrl=url
Status Code:302 Found

Another option is to actually pass the value to the controller: 另一种选择是将值实际传递给控制器​​:

public class BaseController: Controller
{
    public ActionResult start(string myAction)
    {
       string localAction = myAction; //myAction is automatically populated.
    }
}

And in your redirect: 并在您的重定向:

return RedirectToAction
  ("Start", "Base", new { myAction = returnUrl  });

Then the BaseController will automatically fetch the parameter, and you don't need to fetch it from the querystring. 然后BaseController将自动获取参数,您无需从查询字符串中获取它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM