While trying to take the URL parameters from a page view and append them to an API call from a Razor web page in an MVC web application I kept on getting the URI encoded version of the ampersand from Context.Request.QueryString rather than the raw one that’s needed to separate parameters. The below snippet is how I managed to embed the correct URL using the Url.Action helper and the Html.Raw utility.


$.ajax({
	url: '@(Html.Raw(Url.Action("Index", "DataCapture") + Context.Request.QueryString))',
	type: 'POST',
	headers: { 'RequestVerificationToken': getAntiForgeryToken({}) },
	dataType: 'text',
	data: { 'responseJson': JSON.stringify(data) },
	success: function () {
		window.location.href = '@Url.Action("ThankYou", "DataCapture")/@ViewContext.RouteData.Values["id"]';
	},
	error: function () {
		location.href = '@Url.Action("Error", "DataCapture")';
	}
});
	

Leave a Reply

Your email address will not be published. Required fields are marked *


Fatal error: Uncaught GuzzleHttp\Exception\ClientException: Client error: `POST https://dc.services.visualstudio.com/v2/track` resulted in a `400 Invalid instrumentation key` response: {"itemsReceived":1,"itemsAccepted":0,"errors":[{"index":0,"statusCode":400,"message":"Invalid instrumentation key"}]} in D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php:113 Stack trace: #0 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Middleware.php(66): GuzzleHttp\Exception\RequestException::create(Object(GuzzleHttp\Psr7\Request), Object(GuzzleHttp\Psr7\Response)) #1 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\promises\src\Promise.php(203): GuzzleHttp\Middleware::GuzzleHttp\{closure}(Object(GuzzleHttp\Psr7\Response)) #2 D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\promises\src\Promise.php(156): GuzzleHttp\Promise\Promise::callHandler(1, Object(GuzzleHttp\P in D:\home\site\wwwroot\wp-content\plugins\application-insights\vendor\guzzlehttp\guzzle\src\Exception\RequestException.php on line 113