Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on an application that uses Angular.js and ASP.NET MVC. The routes for angular.js contain a '#'. Some pages in the application can be viewed without needing to log in. These pages however contain URL's to pages in the application that need the user to be logged in. They look like:

<a href="~/#/shop/voucher/6bc1">Want this voucher?</a>

So when these links are clicked, MVC forms authentication redirects to the sign in page with the return URL query string that looks like this:

http://localhost:18030/signin?ReturnUrl=%2f#/shop/voucher/6bc1

So on the sign in pages, when i try to look at the query string I only get '/' for return URL as that is the %2f part. I lose the remaining value because of the #.

If I try to URL encode the link, the # and other '/' characters do get encoded but then the routing gets messed up and I get 404 errors.

Is there anyway to access the full query string value with the '#' in it? I am not sure why forms authentication does not encode the #.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

The url fragment is never sent to the server, so you have to get a little creative here.

One way I have solved this in the past is to use client side code to modify the action URL for the login, and append the fragment as a query string parameter.

//Pulls out everything past the '#'
var fragment = $location.path();

$scope.fragmentParam = "&fragment=" + fragment;

You can append that query string param to your form action, and then handle it on the server side in order to redirect the user appropriately.

public ActionResult Login(MyLoginInfo info, string returnUrl, string fragment){

    //Normal login code

    var redirectUrl = String.Format("{0}#{1}", returnUrl, fragment);    

    Redirect(redirectUrl);

}

This is a simplified version, but you should be able to get the general idea. You need to pass that information to the server, and then reconstruct a redirect url that includes the fragment.

share|improve this answer
    
Thank you! Your answer got me thinking in the right direction. Got it working. :) –  Gloria Jun 30 at 20:25
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.