-2

I want to accept my querystring value in MVC Controller.I tried to take it as a parameter also but it also not accept.Because i wanted to confirm this login request comes from inbox.

http://localhost:58692/Account/Login?returnFrom=#inbox

My MVC controller

[HttpPost]
[AllowAnonymous]
public ActionResult Login(User user, string returnUrl) 
{
    if (Request.QueryString["returnFrom"] != null) // this comes as a null
    {

    }
}
ataravati
  • 8,891
  • 9
  • 57
  • 89
TechGuy
  • 4,298
  • 15
  • 56
  • 87
  • 1
    I dont know if querystring parameters are passed when you have a POST method on controller instead of GET. You could use returnFrom as one of the form hidden field in that case. – shahkalpesh Jul 18 '15 at 18:32
  • In the login i want it to confirm this request is normal or this request is comes from Email/Inbox – TechGuy Jul 18 '15 at 18:34
  • In that case, you could have another method/link that can be part of your controller that deals with logging in if it comes from a link/inbox. – shahkalpesh Jul 18 '15 at 18:35
  • @shahkalpesh could you please show me some example pls – TechGuy Jul 18 '15 at 18:48
  • It's not clear what you want to do. You can't use a query string in a POST request. If you want to pass a parameter t your action, just use it as a hidden input in your form. – ataravati Jul 18 '15 at 18:50
  • @TechGuy: I dont have a handy example but you could use a different method that allows GET with a guid as a querystring. That guid can contain reference to user name, let the user enter the password. I am basing this based on emails sent by sites when someone registers and gets activation link in the email. – shahkalpesh Jul 18 '15 at 18:55
  • @ataravati cant use hidden input know ? because this click comes from email – TechGuy Jul 19 '15 at 02:55
  • If it comes from email, then it can't be a POST request. – ataravati Jul 19 '15 at 15:33

2 Answers2

1

It doesn't work because everything after # is client side. The # in the URL defines a link to a named anchor, The query string behind the # will not be sent to IIS server. As this is client side you need to escape the # from the URL - you can't get it on the server because the browser's already stripped it off. try this:

http://localhost:58692/Account/Login?returnFrom=inbox

For doing that you need to write some JavaScript codes, So you can use this technique.

Community
  • 1
  • 1
Sirwan Afifi
  • 10,654
  • 14
  • 63
  • 110
0

You need to have two Login actions (GET and POST):

[AllowAnonymous]
public ActionResult Login(string returnUrl) 
{
    if (Request.QueryString["returnFrom"] != null) 
    {
        // return the Login view... 
    }
}

[HttpPost]
public ActionResult Login(User user) 
{
    // Check if the user is authorized...
}

And, as Sirwan said, you need to remove # from the URL:

http://localhost:58692/Account/Login?returnFrom=inbox
ataravati
  • 8,891
  • 9
  • 57
  • 89