简体   繁体   中英

How to read querystring that contains a # in ASP.NET MVC?

I have a page that is using AngularJS for the routes and ASP.NET MVC as a backend.

When some of my users send a request like the following:

http://mysite/myApp/Accounts/Login?ReturnUrl=%2fmyApp%2f#/myspa1

I want them to login first and then be redirected to ASP.NET MVC to redirect them to the appropiate angular route:

Unfortunately in the ASP.NET MVC controller I cannot see any value passed after the # sign using Request.QueryString["ReturnUrl"] .

Any idea how can I solve this issue?

The part of a URL after that is called a "fragment." It is not sent to servers, because it is meant to cue actions in JavaScript or the browser. More on that here .

If you aren't in fact intending for this to represent a fragment, it should be escaped (with a %23 ) by whomever is filing the request.

Otherwise, you'll have to use a JavaScript function to send the data to the server, but that's not a particularly nice, let alone universal, solution.

In your particular example, http://mysite/myApp/Accounts/Login?ReturnUrl=%2fmyApp%2f#/myspa1 , I'm unsure of what exactly you're looking for.

I'm assuming, however, that you want the ReturnUrl parameter to map to /myApp/#/myspa1 ? In that event, the redirecting agent should be URL-escaping the full parameter address, and not appending anything post-facto.

On the other hand, if your ReturnUrl is meant to represent only /myApp/ , and the fragment portion is meant to be passed to the login page, you should handle that in JavaScript as need be, as it will not be immediately sent to the server.

Edit:

I see this was posted in the comments as a solution, so I'll append what exactly "JavaScript as need be" is referring to. This answer is of particular use here.

In essence, the idea is to capture the URL fragment when the page goes to perform a post-back to the server, and store it in a server-side <asp:HiddenField .../> control.

For example,

<asp:HiddenField runat="server" ID="URLFragmentValue" />

<script>
    $("form").submit(function() {
        $('#<%= URLFragmentValue.ClientID %>').val(window.location.hash);
    });
</script>

Or the non-jQuery form of that script:

<form onsubmit="setUrlFragment()"> ... </form>

<script>
    function setUrlFragment() {
        document.getElementById('<%= URLFragmentValue.ClientID %>').value = window.location.hash;
    });
</script>

That's the gist of it. That answer also gives an example of how to retrieve and inject the hash fragment on the page's load, but that doesn't seem relevant to the scope of your question in particular.

When the page hits your server, you can check URLFragmentValue.Value and, with luck, you'll be able to view and manipulate your fragment value.

But again, I should stress that you should make sure you're using fragments correctly. There are times, for instance in the event of a post-back, where you do need to do things like this. But there are also times when fragments are misused and this is a means to enabling said misuse. Be sure you aren't on that side of things. Understand the implications and have good and justifiable reasoning before implementing what could be perceived conditionally as a Band-Aid to this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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