var querystringFromUrlReferrer= HttpUtility.ParseQueryString(HttpContext.Request.UrlReferrer.Query); var valueYouWant= querystringFromUrlReferrer["NameOfVarToGetFromQuerystring"];
There you go – very helpful…
var querystringFromUrlReferrer= HttpUtility.ParseQueryString(HttpContext.Request.UrlReferrer.Query); var valueYouWant= querystringFromUrlReferrer["NameOfVarToGetFromQuerystring"];
There you go – very helpful…
In a codebase I work in, NHibernate needs an HttpContext to get its current session – like this:
public static ISession GetCurrentSession()
{
var context = HttpContext.Current;
var currentSession = context.Items[CurrentSessionKey] as ISession;
...
To get my hands on a session for a NUnit test – I put this in the setup:
HttpContext.Current = new HttpContext(
new HttpRequest(null, "http://tempuri.org", null),
new HttpResponse(null));
Also, I wipe the context in the teardown:
<code>
[TearDown]
public void TearDown()
{
HttpContext.Current = null;
}
</code>
Credit for this goes to Caio Proiete:
http://caioproiete.net/en/fake-mock-httpcontext-without-any-special-mocking-framework/
Here is a simple way to download a file to a .net user control from a newer MVC controller.
The Controller code:
public FileResult DownloadSweetFile()
{
var downloadDirectory = _appSettings.PathToFile;
var filePathAndName = Path.Combine(downloadDirectory, "MySweetFile.pdf");
var cd = new System.Net.Mime.ContentDisposition
{
FileName = "MySweetFile.pdf",
Inline = false, //NOTE: This forces always prompting the user to download, not open file in the browser
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(filePathAndName, "application/pdf");
}
I used a button on the page to fire the javascript
button type="button" id="btnSweetDownload" onclick="downloadSweetFile();" Click here to download a Sweet File!
(I removed the button tag so this will show correctly)
And here is the javascript to use in the .ascx file:
function downloadSweetFile() {
var url = '/MyControllerName/DownloadSweetFile';
window.location = url;
};
In javascript, I wanted to pass a page’s querystring on to a new page without any changes.
‘location.search’ gives you the querystring verbatim.