1

I am writing an asp.net MVC Application. I have the application send a request to FreeAgent and if the request is successful a code is returned in the redirect of the URL. For example this is a copy of a successful URL.

{
http://localhost:3425/FreeAgent/Home?code=144B2ymEKw3JfB9EDPIqCGeWKYLb9IKc-ABI6SZ0o&state=
}

They have added the ?code=144B2ymEKw3JfB9EDPIqCGeWKYLb9IKc-ABI6SZ0o&state= to my URL I need the bit after the ?code= and before &state=

I can use this to get the URL

string code = Request.Url.AbsoluteUri;

but I need help extracting the code from this

edit: The code will be different each time it is run

1 Answer 1

2

You can use the System.Uri and System.Web.HttpUtility classes

string uri = "http://localhost:3425/FreeAgent/Home?code=144B2ymEKw3JfB9EDPIqCGeWKYLb9IKc-ABI6SZ0o&state=";
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);

Then the value of the code query parameter will be available in queryDictionary["code"]

Sign up to request clarification or add additional context in comments.

2 Comments

So continuing from your example how would i just put: string code = queryDictionary["code"] ?
Yeah, exactly. You can access the query parameters like a dictionary and set them to other variables or do whatever you need.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.