Search code examples
c#routesasp.net-coreasp.net-core-mvcrequest.querystring

Return url functionality


I'm using ASP.NET Core MVC with C#.

I have a functionality on my Login controller to take a parameter called ReturnUrl. The Idea is, that if this is set when the user logs in then I can redirect them back to the page. Straight forward enough.

The code looks like this:

[HttpGet]
[AllowAnonymous]
public IActionResult Login(string returnUrl = null)
{
     ...
}

However, the login functionality is handled by MVC and the main content is using Angular (version 1 not 2). Routes in my app are (for example) http://localhost/#/location1.

If I hit my MVC login page with (again, just an example) http://localhost/Login/?returnUrl=http://localhost/#/location1, then all I get in my returnUrl parameter is http://localhost/. It drops anything from the # symbol onwards.

Any ideas how to stop the code automatically escaping at the # symbol and taking the full url as the returnUrl object?


Solution

  • if you notice in your browser's debug tools, the browser would not be sending this # and any text after it to the server, so the server wouldn't know about its existence at all.

    So instead of something like below,

    http://localhost/Login/?returnUrl=http://localhost/#location1

    do this (note the encoding of # into %23 in the url)

    http://localhost/Login/?returnUrl=http://localhost/%23location1