John
John

Reputation: 339

Access parameters from a URL in ActionResult

I am receiving the following URL and the Callback view from the ProcessToken controller is being displayed:

http://localhost:4151/ProcessToken/CallBack#token_type=Bearer&access_token=54EXXXXXXXXX3GE4

In Callback ActionResult code, I want to access the token_type and access_token.

How do I do this?

Upvotes: 0

Views: 509

Answers (4)

SebastianStehle
SebastianStehle

Reputation: 2459

It is part of the fragment and therefore you cannot access it at server side.

Read this: https://www.rfc-editor.org/rfc/rfc2396#section-4

When a URI reference is used to perform a retrieval action on the
identified resource, the optional fragment identifier, separated from
the URI by a crosshatch ("#") character, consists of additional
reference information to be interpreted by the user agent after the
retrieval action has been successfully completed.  As such, it is not
part of a URI, but is often used in conjunction with a URI.

You can access query-string-parameters with model-binding as ken4z pointed out.

You can read the hash-part with jquery:

var data = window.location.hash.substring(1);
$.post('[YOUR_URL]', data, function () {
    // SUCCEEDED, redirect or so
});

Upvotes: 1

dmarlow
dmarlow

Reputation: 427

Like ken4z says, you should use parameters to model bind. Otherwise, you can use Request.QueryString to access them.

Upvotes: 0

Van
Van

Reputation: 1387

do what ken4z suggested, if not, you can always read the query strings from the Request object.

Request.QueryString["token_type"]
Request.QueryString["access_token"]

Upvotes: 1

ken4z
ken4z

Reputation: 1390

Please provide more information and an example of what you think the ActionResult method should be. But based on what I think you are asking:

public ActionResult CallBack(string token_type, string access_token){ //Do Something }

Upvotes: 1

Related Questions